Fork me on github

【每日一题】12. 整数转罗马数字

https://leetcode-cn.com/problems/integer-to-roman/

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

posted @ 2021-05-14 12:51  zjy4fun  阅读(39)  评论(0编辑  收藏  举报