LeetCode - 12.Integer to Roman

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9.
  • X can be placed before L (50) and C (100) to make 40 and 90.
  • C can be placed before D (500) and M (1000) to make 400 and 900.

Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.

Example 1:

Input: 3
Output: "III"
真?暴力法

Runtime: 5 ms, faster than 55.25%

class Solution {
    public String intToRoman(int num) {
        if (num > 3999 || num < 1){
            return null;
        }
        String romanPieces[]={"","I","II","III","IV","V","VI","VII","VIII","IX",
                "","X","XX","XXX","XL","L","LX","LXX","LXXX","XC",
                "","C","CC","CCC","CD","D","DC","DCC","DCCC","CM",
                "","M","MM","MMM"};
        return romanPieces[num/1000+30]+romanPieces[(num/100)%10+20]
                +romanPieces[(num/10)%10+10]+romanPieces[num%10];
    }
}
偽?暴力法

Runtime: 3 ms, faster than 100.00%

class Solution {
    public String intToRoman(int num) {
        int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
        String[] romanPieces = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < values.length; i++) {
            while (num >= values[i]) {
                num -= values[i];
                sb.append(romanPieces[i]);
            }
        }
        return sb.toString();
    }
}
正常計算

Runtime: 4 ms, faster than 79.40%

class Solution {
    public String intToRoman(int num) {
        StringBuilder sb = new StringBuilder();
        String[] roman = { "I", "V", "X", "L", "C", "D", "M" };
        for (int i = 3,digitNum = 0,temp = 1000; i >= 0; i--,temp /= 10) {
            digitNum = (num / temp) %10;
            switch (digitNum) {
                case 9:
                    sb.append(roman[2 * i]).append(roman[2 * i + 2]);
                    break;
                case 4:
                    sb.append(roman[2 * i]).append(roman[2 * i + 1]);
                    break;
                default:
                    if (digitNum >= 5){
                        sb.append(roman[2 * i + 1]);
                    }
                    for (int j = 0; j < digitNum % 5; j++){
                        sb.append(roman[2 * i]);
                    }
                    break;
            }
        }
        return sb.toString();
    }
}
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,436評論 0 10
  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,850評論 0 23
  • Type:medium Roman numerals are represented by seven diffe...
    萌小熙喵閱讀 216評論 0 0
  • 借一盞午夜街頭,昏黃燈光 照亮那坎坷路上人影一雙 借一寸三九天里,冽冽暖陽 融這茫茫人間刺骨涼 借一泓古老河水,九...
    老蔣689閱讀 289評論 7 32
  • 我走在路的兩旁 左走也不是 右走也不是 人人走向同樣的方向 而我一路走向黑 無邊的黑暗包圍著我 我看到了光 一倏而...
    伊人兒閱讀 285評論 0 1