罗马数字转整数 · Roman to Integer
13.?Roman to Integer
[抄題]:
?[暴力解法]:
時間分析:
空間分析:
[思維問題]:
[一句話思路]:
[輸入量]:空:?正常情況:特大:特小:程序里處理到的特殊情況:異常情況(不合法不合理的輸入):
[畫圖]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
? [五分鐘肉眼debug的結果]:
[總結]:
toInt函數要用。否則不能直接給字母比大小
[復雜度]:Time complexity: O(n) Space complexity: O(n)
[英文數據結構或算法,為什么不用別的數據結構或算法]:
直接背英文對應的數字就行了
[其他解法]:
[Follow Up]:
[LC給出的題目變變變]:
?[代碼風格] :
public class Solution {/** @param s: Roman representation* @return: an integer*/public int romanToInt(String s) {char[] sc = new char[s.length()];sc = s.toCharArray();int ans = toInt(sc[0]);for (int i = 1; i < s.length(); i++) {ans += toInt(sc[i]);if (toInt(sc[i - 1]) < toInt(sc[i])) {ans -= 2 * toInt(sc[i - 1]);}}return ans;}//toIntprivate int toInt (char s) {switch(s) {case 'I':return 1;case 'V':return 5;case 'X':return 10;case 'L':return 50;case 'C':return 100;case 'D':return 500;case 'M':return 1000;}return 0;} } View Code?
?整數轉羅馬字
[抄題]:
?[暴力解法]:
時間分析:
空間分析:
[思維問題]:
覺得可能有很多種分解方法:應該數位分離,把千百十位分別挑出來,就只有一種了
[一句話思路]:
[輸入量]:空:?正常情況:特大:特小:程序里處理到的特殊情況:異常情況(不合法不合理的輸入):
[畫圖]:
[一刷]:
[二刷]:
[三刷]:
[四刷]:
[五刷]:
? [五分鐘肉眼debug的結果]:
[總結]:
[復雜度]:Time complexity: O() Space complexity: O()
[英文數據結構或算法,為什么不用別的數據結構或算法]:
無
[其他解法]:
[Follow Up]:
[LC給出的題目變變變]:
273.?Integer to English Words 差不多,小于20的數單獨列出來即可
?[代碼風格] :
public class Solution {/*** @param n: The integer* @return: Roman representation*/public String intToRoman(int n) {String M[] = {"", "M", "MM", "MMM"};String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};//XLString I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};return M[(n / 1000) % 10] + C[(n / 100) % 10] + X[(n / 10) % 10] + I[n % 10];} } View Code?
轉載于:https://www.cnblogs.com/immiao0319/p/8448799.html
總結
以上是生活随笔為你收集整理的罗马数字转整数 · Roman to Integer的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Web API之service work
- 下一篇: rabbitmq 一些基本函数介绍