Leetcode 12. Integer to Roman
方法1: 這題我們可以列舉出所有羅馬數字的組合,然後遍歷num。時間複雜1,因為最多output長度為15,是一個定值。空間複雜1.
class Solution {
public String intToRoman(int num) {
int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
String[] symbols = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
StringBuilder sb = new StringBuilder();
while(num > 0){
for(int i = 0; i < values.length; ++i){
if(values[i] <= num){
sb.append(symbols[i]);
num -= values[i];
break;
}
}
}
return sb.toString();
}
}
總結:
- 無
相關文章
- Leetcode 12 Integer to RomanLeetCode
- Leetcode 13 Roman to IntegerLeetCode
- LeetCode Integer to Roman(012)解法總結LeetCode
- LeetCode Roman to Integer(013)解法總結LeetCode
- LeetCode 13. Roman to Integer C語言LeetCodeC語言
- LeetCode - 解題筆記 - 12 - Integer to RomanLeetCode筆記
- 13. Roman to Integer
- Roman to Integer 羅馬數字轉整數
- Leetcode 7 Reverse IntegerLeetCode
- Leetcode 273 Integer to English WordsLeetCode
- Leetcode 8 String to Integer (atoi)LeetCode
- LeetCode Reverse Integer(007)解法總結LeetCode
- [leetcode] 1394. Find Lucky Integer in an ArrayLeetCode
- LeetCode String to Integer (atoi)(008)解法總結LeetCode
- LeetCode - 解題筆記 - 7 - Reverse IntegerLeetCode筆記
- Leetcode 8. String to Integer (atoi) 字串轉整數 (atoi)LeetCode字串
- 12.設計模式設計模式
- Integer比較
- 12.索引合併-indexMerge索引Index
- Integer包裝類
- [Java基礎]IntegerJava
- Integer的比較
- 12.在vue中插槽(slot)Vue
- 12. Vue搭建本地服務Vue
- 程式碼審查清單:Java併發 - Roman LeventovJava
- 走進 JDK 之 IntegerJDK
- Integer128==128?falseFalse
- ibatis中integer型別BAT型別
- 【重溫基礎】12.使用物件物件
- 12. 記憶體管理(Memory Management)記憶體
- 12. Redis叢集環境搭建Redis
- 12. 斷點續傳的原理斷點
- 12.享元模式設計思想模式
- Java Integer的快取策略Java快取
- int和Integer的區別
- Integer 自動拆箱封箱
- Integer轉int出現NullPointExceptionNullException
- int與Integer的區別