Work in my FireFox,but got a TypeError in line 34?


  • 0
    M
    var ladderLength = function(beginWord, endWord, wordList) {
        var len = beginWord.length;
        var n = Number.MAX_VALUE;
        var wordSimaler = function(word,wordlist){
            var words = [];
            for(var i in wordlist){
                var num = 0;
                for(var j = 0 ; j < len; j++){
                    if(wordlist[i][j] != word[j])
                        num++;
                    if(num > 1)
                       continue;
                }
                if(num == 1)
                    words.push(wordlist[i]);
           }
           return words;
        }
    
    
        function backtracking(beginWord,wordlist,leng){
            var similarWords = wordSimaler(beginWord, wordlist);
            if(similarWords.indexOf(endWord) !== -1){
                if(n > leng)
                    n = leng + 1;
            } else {
                for(var i in similarWords){
                    backtracking(similarWords[i],
                             wordlist.splice(wordlist.indexOf(similarWords[i])) ,
                             leng+1);
                }
            }
        
        }
        wordList.push(endWord);
        backtracking(beginWord ,wordList ,1);
        if(n === Number.MAX_VALUE)
           return 0;
        return n;
    };

Log in to reply
 

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