Javascript 1 index 92ms


  • 0
    R
    var shortestDistance = function(words, word1, word2) {
        var index= -1, minDistance = Number.MAX_SAFE_INTEGER;
        for(var i = 0; i < words.length; i++) {
            if(words[i] === word1 || words[i] === word2) {
                if(index != -1 && words[index] !== words[i]) {
                    minDistance = Math.min(minDistance, i - index);
                }
                index = i;
            }
        }
        return minDistance;
    };
    

Log in to reply
 

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