Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit93e6ca6

Browse files
committed
add: Word Ladder
1 parent72bcb2a commit93e6ca6

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ This is the solutions collection of my LeetCode submissions, most of them are pr
4040
|91|[Decode Ways](https://leetcode.com/problems/decode-ways/)|[JavaScript](./src/decode-ways/res.js)|Medium|
4141
|93|[Restore IP Addresses](https://leetcode.com/problems/restore-ip-addresses/)|[JavaScript](./src/restore-ip-addresses/res.js)|Medium|
4242
|120|[Triangle](https://leetcode.com/problems/triangle/)|[JavaScript](./src/triangle/res.js)|Medium|
43+
|127|[Word Ladder](https://leetcode.com/problems/word-ladder/)|[JavaScript](./src/word-ladder/res.js)|Medium|
4344
|130|[Surrounded Regions](https://leetcode.com/problems/surrounded-regions/)|[JavaScript](./src/surrounded-regions/res.js)|Medium|
4445
|133|[Clone Graph](https://leetcode.com/problems/clone-graph/)|[JavaScript](./src/clone-graph/res.js)|Medium|
4546
|136|[Single Number](https://leetcode.com/problems/single-number/)|[JavaScript](./src/single-number/res.js)|Easy|

‎src/word-ladder/res.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/**
2+
* res.js
3+
*@authors Joe Jiang (hijiangtao@gmail.com)
4+
*@date 2017-06-21 23:22:33
5+
*
6+
* 127. Word Ladder
7+
* Problem: https://leetcode.com/problems/word-ladder/
8+
* Discuss: https://discuss.leetcode.com/category/135/word-ladder
9+
*
10+
* * Traditional
11+
* * Two-end BFS
12+
* * Dijkstra's algorithm
13+
*
14+
*@param {string} beginWord
15+
*@param {string} endWord
16+
*@param {string[]} wordList
17+
*@return {number}
18+
*/
19+
letladderLength=function(beginWord,endWord,wordList){
20+
wordList.push(endWord);
21+
lettoVisit=newSet(),
22+
wordDict=newSet(wordList),
23+
dist=2;
24+
25+
if(wordList.length===wordDict.size)return0;
26+
addNextWords(beginWord,wordDict,toVisit);
27+
28+
while(toVisit.size>0){
29+
letentriesLen=toVisit.size,
30+
mark=0;
31+
32+
for(letitemoftoVisit.values()){
33+
if(mark++===entriesLen)break;
34+
toVisit.delete(item);
35+
36+
if(item===endWord)returndist;
37+
addNextWords(item,wordDict,toVisit);
38+
}
39+
40+
dist++;
41+
}
42+
43+
return0;
44+
};
45+
46+
letaddNextWords=function(word,wordDict,toVisit){
47+
wordDict.delete(word);
48+
letlen=word.length;
49+
50+
for(leti=0;i<len;i++){
51+
for(letj=97;j<123;j++){
52+
lettmp=word.substring(0,i)+String.fromCharCode(j)+word.substring(i+1,len);
53+
if(wordDict.has(tmp)){
54+
toVisit.add(tmp);
55+
wordDict.delete(tmp);
56+
}
57+
}
58+
}
59+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp