Simple 6ms Java Solution (also beats 100% in c, c++, python and js by a lot)


  • 0
    F

    I just loop once throug the string, no extra space needed except for a couple local variables, and one of them is the last char that I use to ask if I need to add the actual value for the current char or the diminished value:
    Times:
    java: 6ms
    c: 12 ms
    c++: 19 ms
    python: 79ms
    c#: 135 ms
    js: 182 ms

    Java:

    public class Solution {
        public int romanToInt(String s) {
            int num = 0;
            char last = 0;
            for(int i = 0; i < s.length(); i++){
                final char c = s.charAt(i);
                switch (c){
                    case 'I': num ++; break;
                    case 'V': 
                        num += (last == 'I')?3:5;
                        break;
                    case 'X':
                        num += (last == 'I')?8:10;
                        break;
                    case 'L':
                        num += (last == 'X')?30:50;
                        break;
                    case 'C':
                        num += (last == 'X')?80:100;
                        break;
                    case 'D':
                        num += (last == 'C')?300:500;
                        break;
                    case 'M':
                        num += (last == 'C')?800:1000;
                        break;
                    default: return 0;
                }
                last = c;
            }
            return num;
        }
    }
    

    C++ (19ms):

    class Solution {
    public:
        int romanToInt(string s) {
            int num = 0, i = 0;
            char last = 0;
            while(true){
                switch (s[i]){
                    case 0  : return num;
                    case 'I': num ++; break;
                    case 'V': 
                        num += (last == 'I')?3:5;
                        break;
                    case 'X':
                        num += (last == 'I')?8:10;
                        break;
                    case 'L':
                        num += (last == 'X')?30:50;
                        break;
                    case 'C':
                        num += (last == 'X')?80:100;
                        break;
                    case 'D':
                        num += (last == 'C')?300:500;
                        break;
                    case 'M':
                        num += (last == 'C')?800:1000;
                        break;
                    default: return 0;
                }
                last = s[i++];
            }
        }
    };
    

    C (12ms):

    int romanToInt(char* s) {
            int num = 0, i = 0;
            char last = 0;
            do{
                switch (s[i]){
                    case 0: return num;
                    case 'I': num++; break;
                    case 'V': 
                        num += (last == 'I')?3:5;
                        break;
                    case 'X':
                        num += (last == 'I')?8:10;
                        break;
                    case 'L':
                        num += (last == 'X')?30:50;
                        break;
                    case 'C':
                        num += (last == 'X')?80:100;
                        break;
                    case 'D':
                        num += (last == 'C')?300:500;
                        break;
                    case 'M':
                        num += (last == 'C')?800:1000;
                        break;
                    default: return 0;
                }
                last = s[i++];
            }while(true);
    }
    

    Python (79ms)

    class Solution(object):
        def romanToInt(self, s):
            num=0;last=0
            for c in s: 
                if c==u'I': num+=1
                elif c==u'V': num+=(last == u'I' and 3 or 5)
                elif c==u'X': num+=(last == u'I' and 8 or 10)
                elif c==u'L': num+=(last == u'X' and 30 or 50)
                elif c==u'C': num+=(last == u'X' and 80 or 100)
                elif c==u'D': num+=(last == u'C' and 300 or 500)
                elif c==u'M': num+=(last == u'C' and 800 or 1000)
                else: return 0;
                last=c
            return num;
    

    c# (135ms):

    public class Solution {
        public int RomanToInt(string s) {
            int num = 0;
            char last = '\x00';
            foreach(char c in s){
                switch (c){
                    case 'I': num++; break;
                    case 'V': 
                        num += (last == 'I')?3:5;
                        break;
                    case 'X':
                        num += (last == 'I')?8:10;
                        break;
                    case 'L':
                        num += (last == 'X')?30:50;
                        break;
                    case 'C':
                        num += (last == 'X')?80:100;
                        break;
                    case 'D':
                        num += (last == 'C')?300:500;
                        break;
                    case 'M':
                        num += (last == 'C')?800:1000;
                        break;
                    default: return 0;
                }
                last = c;
            }
            return num;
        }
    }
    

    Javascript (182ms):

    var romanToInt = function(s) {
        var num = 0;
        var last = 0;
        for(var i = 0, l = s.length; i < l; i++){
            var c = s.charCodeAt(i) & 95; //simple uppercase
            switch (c){
                case 73: //I
                    num ++; break;
                case 86: //'V': 
                    num += (last == 73)?3:5;
                    break;
                case 88: //'X':
                    num += (last == 73)?8:10;
                    break;
                case 76: //'L':
                    num += (last == 88)?30:50;
                    break;
                case 67: //'C':
                    num += (last == 88)?80:100;
                    break;
                case 68: //'D':
                    num += (last == 67)?300:500;
                    break;
                case 77: //'M':
                    num += (last == 67)?800:1000;
                    break;
                default: return 0;
            }
            last = c;
        }
        return num;
    };
    

Log in to reply
 

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