Java Solution easy to understand


  • 0
    X

    explanation:
    The Rome numeral consists of only 7 letters:
    字母
    M->1000
    D->500
    C->100
    L->50
    X->10
    V->5
    I->1
    Four rules:
    1.Digital ligatures the same, that is equal to the number of these numbers is the sum of the number of. For example, XXX means 30
    2.Small numbers are on the right hand side of large numbers. The number represented is equal to the sum of these numbers, such as VIII, which means 8
    3.A small number (limited to I, X, C) on the left side of the larger number, the number represented is equal to the big number minus the smaller number: IV, for example, means 4
    4.Draw a horizontal line on a number indicating that the figure is 1000 times worth (because the subject takes only 4000 or less, so this rule does not have to be considered).

    Five rules of group number
    1.I, X, C: you can only use 3 at most. If you put it on the left side of the big number, you can only use 1.
    2.V, L, D: cannot be placed on the left side of the large number, only one.
    3.I can only be used on the left side of V and X. IV stands for 4, and IX stands for 9
    4.X can only be placed on the left side of L, C. XL stands for 40, and XC stands for 90
    5.C can only be used on the left side of D, M. CD stands for 400, and CM stands for 900

    solution:

     class Solution {
         public int romanToInt(String s) {
            int result = 0;
            for(int i = 0;i <= s.length()-2;i++) {
                switch(s.charAt(i)){
                case 'M': result = result + 1000;break;
                case 'D':result = result + 500;break;
                case 'C': 
                    if((s.charAt(i + 1) != 'D')&&(s.charAt(i + 1) != 'M')) {
                       result = result + 100;break; 
                    }
                    else {
                        result = result -100;break;
                    }
                case 'L':result = result + 50;break;
                case 'X': 
                    if((s.charAt(i + 1) != 'L')&&(s.charAt(i + 1) != 'C')) {
                       result = result + 10;break;
                    }
                    else {
                        result = result - 10;break;
                    }
                    
                case 'V':result = result + 5;break;
                case 'I':
                    if((s.charAt(i + 1) != 'V')&&(s.charAt(i + 1) != 'X')) {
                       result = result + 1;break;
                    }
                    else {
                        result = result - 1;break;
                    }
                default: System.out.println("输入有误");
            }
            }
            switch(s.charAt(s.length()-1)){
                case 'M': result = result + 1000;break;
                case 'D':result = result + 500;break;
                case 'C': result = result + 100;break; 
                case 'L':result = result + 50;break;
                case 'X': result = result + 10;break;
                case 'V':result = result + 5;break;
                case 'I':result = result + 1;break;
                default: System.out.println("输入有误");
            }
            return result;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.