class Solution {
int n; // 单词个数 + 1(开头单词)
int m; // 单词长度
List<Integer>[] g; // 记录两个单词是否可以满足条件
boolean[] v;
int endInd = -1; // 记录结束位置的下标
public int diff(String word1, String word2) { // 判断变换次数的函数
int dif = 0;
for (int i = 0; i < word1.length(); i ++) {
if (word1.charAt(i) != word2.charAt(i)) dif ++;
}
return dif;
}
public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
List<List<String>> ans = new ArrayList();
n = wordList.size() + 1;
m = endWord.length();
g = new ArrayList[n];
v = new boolean[n];
for (int i = 0; i < n; i ++) {
g[i] = new ArrayList<Integer>();
}
for (int i = 0; i < wordList.size(); i ++) {
if (diff(beginWord, wordList.get(i)) == 1) { // 和开头单词是否能够互通
g[n - 1].add(i);
g[i].add(n - 1);
}
if (diff(endWord, wordList.get(i)) == 0) endInd = i;
for (int j = i + 1; j < wordList.size(); j ++) {
if (diff(wordList.get(j), wordList.get(i)) == 1) {
g[j].add(i);
g[i].add(j);
}
}
}
if (endInd == -1) return ans;
List<List<String>>[] path = new ArrayList[n]; // 记录到每个点的最短路径
for (int i = 0; i < n; i ++) {
path[i] = new ArrayList();
}
List<String> cur = new ArrayList<String>();
cur.add(beginWord);
path[n - 1].add(cur);
int[] d = new int[n]; // 记录到每个点的最短距离
Arrays.fill(d, Integer.MAX_VALUE);
d[n - 1] = 0;
Queue<Integer> q = new LinkedList<Integer>();
q.add(n - 1);
v[n - 1] = true;
while (!q.isEmpty()) {
int top = q.poll();
if (d[top] >= d[endInd]) continue;
for (int k = 0; k < g[top].size(); k ++) {
int i = g[top].get(k);
if (d[i] != Integer.MAX_VALUE && d[i] > d[endInd]) {
v[i] = true;
continue;
}
if (d[i] >= d[top] + 1) { // 经过这个top这个点到i有最短路径
d[i] = d[top] + 1;
for (int j = 0; j < path[top].size(); j ++) {
cur = new ArrayList(path[top].get(j));
cur.add(wordList.get(i));
path[i].add(cur);
}
}
if (!v[i]) {
v[i] = true;
q.add(i);
}
}
}
return path[endInd];
}
}
改进一点点,还是很慢
class Solution {
int n; // 单词个数 + 1(开头单词)
int m; // 单词长度
List<Integer>[] g; // 记录两个单词是否可以满足条件
boolean[] v;
int endInd = -1; // 记录结束位置的下标
List<List<String>> ans;
List<Integer>[] pa;
public int diff(String word1, String word2) { // 判断变换次数的函数
int dif = 0;
for (int i = 0; i < word1.length(); i ++) {
if (word1.charAt(i) != word2.charAt(i)) dif ++;
}
return dif;
}
public void findPath(List<String> wordList, List<String> cur, int fro, String beginWord) {
// System.out.println(fro);
if (fro == n - 1) {
cur.add(beginWord);
Collections.reverse(cur);
ans.add(cur);
return;
}
cur.add(wordList.get(fro));
for (int i = 0; i < pa[fro].size(); i ++) {
findPath(wordList, new ArrayList(cur), pa[fro].get(i), beginWord);
}
}
public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {
ans = new ArrayList();
n = wordList.size() + 1;
m = endWord.length();
g = new ArrayList[n];
v = new boolean[n];
for (int i = 0; i < n; i ++) {
g[i] = new ArrayList<Integer>();
}
for (int i = 0; i < wordList.size(); i ++) {
if (diff(beginWord, wordList.get(i)) == 1) { // 和开头单词是否能够互通
g[n - 1].add(i);
g[i].add(n - 1);
}
if (diff(endWord, wordList.get(i)) == 0) endInd = i;
for (int j = i + 1; j < wordList.size(); j ++) {
if (diff(wordList.get(j), wordList.get(i)) == 1) {
g[j].add(i);
g[i].add(j);
}
}
}
if (endInd == -1) return ans;
pa = new ArrayList[n]; // 记录每个点的父亲
for (int i = 0; i < n; i ++) {
pa[i] = new ArrayList<Integer>();
}
int[] d = new int[n]; // 记录到每个点的最短距离
Arrays.fill(d, Integer.MAX_VALUE);
d[n - 1] = 0;
Queue<Integer> q = new LinkedList<Integer>();
q.add(n - 1);
v[n - 1] = true;
while (!q.isEmpty()) {
int top = q.poll();
if (d[top] >= d[endInd]) continue;
for (int k = 0; k < g[top].size(); k ++) {
int i = g[top].get(k);
if (d[i] != Integer.MAX_VALUE && d[i] > d[endInd]) {
v[i] = true;
continue;
}
if (d[i] >= d[top] + 1) { // 经过这个top这个点到i有最短路径
d[i] = d[top] + 1;
pa[i].add(top); // 给i点添加父亲
}
if (!v[i]) {
v[i] = true;
q.add(i);
}
}
}
findPath(wordList, new ArrayList(), endInd, beginWord);
return ans;
}
}