leetcode 12. Integer to Roman

Roman numerals are represented by seven different symbols: IVXLCD and M.

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9. 
  • X can be placed before L (50) and C (100) to make 40 and 90. 
  • C can be placed before D (500) and M (1000) to make 400 and 900.

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: L = 50, V = 5, III = 3.

Example 5:

Input: 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

 

解法一:我的解法

把个十百千都拆成数字,然后分类讨论 为了使分类讨论看起来不要那么死板,强行塞入了循环...

class Solution {
    public String intToRoman(int num) {
        int w4 = num % 10;
        int w3 = (num/10) % 10;
        int w2 = (num/100) % 10;
        int w1 = (num/1000) % 10;
        String res = "";
        
        if(w1>0) {
            for(int i=0; i<w1; i++) res += "M";
        }
        
        if(w2>0 && w2<4) {
            for(int i=0; i<w2; i++) res += "C";
        }else if(w2 == 4) {
            res += "CD";
        }else if(w2 == 9) {
            res += "CM";
        }else if(w2 != 0){
            res += "D";
            for(int i=0; i<w2-5; i++) res += "C";
        }
        
        if(w3>0 && w3<4) {
            for(int i=0; i<w3; i++) res += "X";
        }else if(w3 == 4) {
            res += "XL";
        }else if(w3 == 9) {
            res += "XC";
        }else if(w3 != 0){
            res += "L";
            for(int i=0; i<w3-5; i++) res += "X";
        }
        
        if(w4>0 && w4<4) {
            for(int i=0; i<w4; i++) res += "I";
        }else if(w4 == 4) {
            res += "IV";
        }else if(w4 == 9) {
            res += "IX";
        }else if(w4 != 0){
            res += "V";
            for(int i=0; i<w4-5; i++) res += "I";
        }
        return res;
       
        
    }
}

 

解法二:

同样是分类讨论,为什么他就可以这么清新脱俗??(但是所用的时间和内存都和我的方法一样)

public static 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];
}

 

上一篇:Roman to Integer -- LeetCode


下一篇:【LeetCode】13. Roman to Integer