登录
  • #刷题
  • #leetcode

wo‌‌‍‍‌‍‌‌‌‌‍‍‌‍‌‍‍‌‍‍‌‌‌‍‌‌‌‌‍‍‌‌rd ladder总过不了一个test case

小水
697
4
code如下,其他case都能通过,不明白为什么对于beginword:"lost", endword:"cost",["most","fist","lost","cost","fish"],到最后不执行res++;[mw_shl_code=java,true]public int ladderLength(String beginWord, String endWord, List<String> wordList) {

Set<String> dict = new HashSet<String>(wordList);

if(dict == null) return 0;

Queue<String> q = new LinkedList<String>();

q.offer(beginWord);

int res = 1;

while(!q.isEmpty()){

int size = q.size();

for(int n = 0; n < size; n++){

String temp = q.poll();

if (temp.equals(endWord)) return res;



for(int i = 0; i < temp.length(); i++){

for(char c = 'a'; c <= 'z'; c++){

String newWord = temp.substring(0, i) + c + temp.substring(i + 1);

if(dict.contains(newWord) && !newWord.equals(temp)){

dict.remove(newWord);

q.offer(newWord);

}

}

}



}

res++;

}

return 0;

}[/mw_shl_code]

补充内容 (2020-3-17 00:40):

public int ladderLength(String beginWord, String endWord, List<String> wordList) {

int[] res = new int[1];

Set<String> dict = new HashSet<String>(wordList);

Set<String> begi...

补充内容 (2020-3-17 00:46):

我贴错code了,有问题的code在三楼……
4条回复
热度排序

发表回复