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

Commite320d72

Browse files
format
1 parent311dd26 commite320d72

File tree

2 files changed

+89
-5
lines changed

2 files changed

+89
-5
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ Your ideas/fixes/algorithms are more than welcome!
444444
|130|[Surrounded Regions](https://leetcode.com/problems/surrounded-regions/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_130.java)| O(?)|O(?)| Medium|
445445
|129|[Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_129.java)| O(n)|O(h) | Medium| DFS
446446
|127|[Word Ladder](https://leetcode.com/problems/word-ladder/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_127.java)| O(?)|O(?) | Medium| BFS
447+
|126|[Word Ladder II](https://leetcode.com/problems/word-ladder-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_126.java)| O(?)|O(?) | Hard| BFS
447448
|125|[Valid Palindrome](https://leetcode.com/problems/valid-palindrome/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_125.java)| O(n)|O(1) | Easy| Two Pointers
448449
|124|[Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_124.java)| O(n)|O(h) | Hard | Tree, DFS
449450
|123|[Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_123.java)| O(?)|O(?)| Hard|

‎src/main/java/com/fishercoder/solutions/_126.java

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
packagecom.fishercoder.solutions;
22

3-
importjava.util.ArrayList;
4-
importjava.util.List;
3+
importjava.util.*;
54

65
/**
76
* 126. Word Ladder II
@@ -32,10 +31,94 @@
3231
You may assume beginWord and endWord are non-empty and are not the same.
3332
*/
3433
publicclass_126 {
34+
/**Reference: https://discuss.leetcode.com/topic/2857/share-two-similar-java-solution-that-accpted-by-oj*/
3535

36-
publicList<List<String>>findLadders(StringbeginWord,StringendWord,List<String>wordList) {
37-
List<List<String>>result =newArrayList<>();
38-
returnresult;
36+
Map<String,List<String>>map;
37+
List<List<String>>results;
38+
39+
publicList<List<String>>findLadders(Stringstart,Stringend,List<String>dict) {
40+
results =newArrayList<>();
41+
if (dict.size() ==0)
42+
returnresults;
43+
44+
intmin =Integer.MAX_VALUE;
45+
46+
Queue<String>queue =newArrayDeque<>();
47+
queue.add(start);
48+
49+
map =newHashMap<>();
50+
51+
Map<String,Integer>ladder =newHashMap<>();
52+
for (Stringstring :dict)
53+
ladder.put(string,Integer.MAX_VALUE);
54+
ladder.put(start,0);
55+
56+
dict.add(end);
57+
//BFS: Dijisktra search
58+
while (!queue.isEmpty()) {
59+
60+
Stringword =queue.poll();
61+
62+
intstep =ladder.get(word) +1;//'step' indicates how many steps are needed to travel to one word.
63+
64+
if (step >min)break;
65+
66+
for (inti =0;i <word.length();i++) {
67+
StringBuilderbuilder =newStringBuilder(word);
68+
for (charch ='a';ch <='z';ch++) {
69+
builder.setCharAt(i,ch);
70+
Stringnew_word =builder.toString();
71+
if (ladder.containsKey(new_word)) {
72+
73+
if (step >ladder.get(new_word)) {//Check if it is the shortest path to one word.
74+
continue;
75+
}elseif (step <ladder.get(new_word)) {
76+
queue.add(new_word);
77+
ladder.put(new_word,step);
78+
}else ;// It is a KEY line. If one word already appeared in one ladder,
79+
// Do not insert the same word inside the queue twice. Otherwise it gets TLE.
80+
81+
if (map.containsKey(new_word)) {//Build adjacent Graph
82+
map.get(new_word).add(word);
83+
}else {
84+
List<String>list =newLinkedList<String>();
85+
list.add(word);
86+
map.put(new_word,list);
87+
//It is possible to write three lines in one:
88+
//map.put(new_word,new LinkedList<String>(Arrays.asList(new String[]{word})));
89+
//Which one is better?
90+
}
91+
92+
if (new_word.equals(end)) {
93+
min =step;
94+
}
95+
96+
}//End if dict contains new_word
97+
}//End:Iteration from 'a' to 'z'
98+
}//End:Iteration from the first to the last
99+
}//End While
100+
101+
//BackTracking
102+
LinkedList<String>result =newLinkedList<>();
103+
backTrace(end,start,result);
104+
105+
returnresults;
106+
}
107+
108+
privatevoidbackTrace(Stringword,Stringstart,List<String>list) {
109+
if (word.equals(start)) {
110+
list.add(0,start);
111+
results.add(newArrayList<>(list));
112+
list.remove(0);
113+
return;
114+
}
115+
list.add(0,word);
116+
if (map.get(word) !=null) {
117+
for (Strings :map.get(word)) {
118+
backTrace(s,start,list);
119+
}
120+
}
121+
list.remove(0);
39122
}
40123

41124
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp