Best java solution))


  • 0
    M
        public int romanToInt(String s) {
            if(s==null || s.trim().isEmpty())
                throw new IllegalArgumentException("Empty input");
            char[] ca = s.toCharArray();
            int result;
            int num = 0;
            for (int i = s.length() - 1; i >= 0; i--) {
                char c = ca[i];
                switch (c) {
                    case 'I':
                        num += 1;
                        break;
                    case 'V':
                        if (i>0 && ca[i - 1] == 'I') {
                            num += 4;
                            i--;
                        } else num += 5;
                        break;
                    case 'X':
                        if (i>0 && ca[i - 1] == 'I') {
                            num += 9;
                            i--;
                        } else num += 10;
                        break;
                    case 'L':
                        if (i>0 && ca[i - 1] == 'X') {
                            num += 40;
                            i--;
                        } else num += 50;
                        break;
                    case 'C':
                        if (i>0 && ca[i - 1] == 'X') {
                            num += 90;
                            i--;
                        } else num += 100;
                        break;
                    case 'D':
                        if (i>0 && ca[i - 1] == 'C') {
                            num += 400;
                            i--;
                        } else num += 500;
                        break;
                    case 'M':
                        if (i>0 && ca[i - 1] == 'C') {
                            num += 900;
                            i--;
                        } else num += 1000;
                        break;
                    default:
                        throw new IllegalArgumentException("Incorrect input");
                }
            }
            return num;
        }
    

Log in to reply
 

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