美文网首页
LeetCode每日一题:integer to roman

LeetCode每日一题:integer to roman

作者: yoshino | 来源:发表于2017-07-05 15:59 被阅读22次

问题描述

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

问题分析

直接穷举即可,看代码就能明白。

代码实现

public String intToRoman(int num) {
        String M[] = {"", "M", "MM", "MMM"};
        String C[] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
        String X[] = {"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"};
        String I[] = {"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
        return M[num / 1000] + C[(num % 1000) / 100] + X[(num % 100) / 10] + I[num % 10];
    }

相关文章

网友评论

      本文标题:LeetCode每日一题:integer to roman

      本文链接:https://www.haomeiwen.com/subject/yxyqhxtx.html