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();
    }
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 12,199评论 0 10
  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,593评论 0 23
  • Type:medium Roman numerals are represented by seven diffe...
    萌小熙喵阅读 1,544评论 0 0
  • 借一盏午夜街头,昏黄灯光 照亮那坎坷路上人影一双 借一寸三九天里,冽冽暖阳 融这茫茫人间刺骨凉 借一泓古老河水,九...
    老蒋689阅读 2,166评论 7 32
  • 我走在路的两旁 左走也不是 右走也不是 人人走向同样的方向 而我一路走向黑 无边的黑暗包围着我 我看到了光 一倏而...
    伊人儿阅读 2,206评论 0 1

友情链接更多精彩内容