Re: 澳洲小美女参加跳水世锦赛,教练惊呆了! (转载)# Joke - 肚皮舞运动
f*e
1 楼
网上的搜索的众说纷纭
https://goo.gl/ebfvvD
public class Solution {
public int ladderLength(String beginWord, String endWord, List
wordList) {
Queue q = new LinkedList<>();
HashSet list = new HashSet<>(wordList);
int level = 0;
q.add(beginWord);
HashSet set = new HashSet<>();
set.add(beginWord);
while(!q.isEmpty()){
int size = q.size();
level++;
for(int i = 0; i < size; i++){
char[] cur = q.remove().toCharArray();
for(int j = 0; j < cur.length; j++){
char temp = cur[j];
for(char c = 'a'; c <= 'z'; c++){
cur[j] = c;
String next = new String(cur);
if(list.contains(next)){
if(next.equals(endWord)){
return level + 1;
}else if(!set.contains(next)){
q.add(next);
set.add(next);
}
}
}
cur[j] = temp;
}
}
}
return 0;
}
}
https://goo.gl/ebfvvD
public class Solution {
public int ladderLength(String beginWord, String endWord, List
wordList) {
Queue
HashSet
int level = 0;
q.add(beginWord);
HashSet
set.add(beginWord);
while(!q.isEmpty()){
int size = q.size();
level++;
for(int i = 0; i < size; i++){
char[] cur = q.remove().toCharArray();
for(int j = 0; j < cur.length; j++){
char temp = cur[j];
for(char c = 'a'; c <= 'z'; c++){
cur[j] = c;
String next = new String(cur);
if(list.contains(next)){
if(next.equals(endWord)){
return level + 1;
}else if(!set.contains(next)){
q.add(next);
set.add(next);
}
}
}
cur[j] = temp;
}
}
}
return 0;
}
}