Leetcode12 Integer to Roman

Posted chason95

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode12 Integer to Roman相关的知识,希望对你有一定的参考价值。

本题没啥难度,就是技巧性的东西。

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

37ms,96.88%.

以上是关于Leetcode12 Integer to Roman的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode12 Integer to Roman

LeetCode12. Integer to Roman

LeetCode 12 Integer to Roman (整数转罗马数字)

[LeetCode] 12. Integer to Roman ☆☆

LeetCode(12) - Integer to Roman

LeetCode 12. Integer to Roman