Why does the JS duration change drastically for the same code? (63.7% beat changes to 16.3%)


  • 0
    D
    /**
     * @param {string} s
     * @return {number}
     */
    var romanToInt = function(s) {
        var d, num;
        
        d = {
            'I': 1,
            'V': 5,
            'X': 10,
            'L': 50,
            'C': 100,
            'D': 500,
            'M': 1000
        };
        
        num = 0;
        for (var i=s.length-1; i>=0; i--) {
            num += d[s[i]];
            if (d[s[i]] > d[s[i-1]]) {
                num -= d[s[i-1]];
                i--;
            }
        }
        return num;
    
    };

Log in to reply
 

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