文章--LeetCode算法--IntegertoRoman

IntegertoRoman

问题描述

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"

  • Example 2:

Input: 4

Output: "IV"

  • Example 3:

Input: 9

Output: "IX"

  • Example 4:

Input: 58

Output: "LVIII"

Explanation: C = 100, L = 50, XXX = 30 and III = 3.

  • Example 5:

Input: 1994

Output: "MCMXCIV"

Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

实现代码

    public class Solution {
        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];
        }
    }
posted @ 2019-07-19 09:58  AI,me  阅读(60)  评论(0)    收藏  举报