LeetCode---12. Integer to Roman

LeetCode—12. Integer to Roman

题目

https://leetcode.com/problems/integer-to-roman/description/
整数转罗马数字。

思路及解法

这道题感觉没什么技术含量。。把特殊情况列出来,一个一个减去就得出了结果。。

代码

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

猜你喜欢

转载自blog.csdn.net/pnnngchg/article/details/82947908