@@ -13,12 +13,11 @@ func ladderLength(beginWord string, endWord string, words []string) int {
1313 isTransedEndWord := false
1414 // cnt 用于记录生成trans的迭代次数
1515 // 其实也是最短路径的长度
16- // TODO: 修改 cnt 为 res
17- cnt := 1
16+ res := 1
1817 var bfs func ([]string , []string )
1918 // 使用 bfs 方法,递归地生成 trans
2019 bfs = func (words , nodes []string ) {
21- cnt ++
20+ res ++
2221 // words 中的 w
2322 // 与 nodes 中的 n ,可以实现 n->w 的转换,
2423 // 则,w 会被放入 newNodes
@@ -36,6 +35,7 @@ func ladderLength(beginWord string, endWord string, words []string) int {
3635
3736 if isTransed {
3837 if w == endWord {
38+ isTransedEndWord = true
3939 return
4040 }
4141 newNodes = append (newNodes , w )
@@ -62,30 +62,25 @@ func ladderLength(beginWord string, endWord string, words []string) int {
6262 return 0
6363 }
6464
65- return cnt
66- }
67-
68- func deepCopy (src []string ) []string {
69- temp := make ([]string , len (src ))
70- copy (temp , src )
71- return temp
65+ return res
7266}
7367
7468// 题目中说了,words 中没有重复的单词,
7569// 所以,beginWord 最多出现一次
7670func deleteBeginWord (words []string , beginWord string ) []string {
77- i := 0
78- for ; i < len ( words ) ; i ++ {
71+ i , size := 0 , len ( words )
72+ for ; i < size ; i ++ {
7973 if words [i ] == beginWord {
8074 break
8175 }
8276 }
8377
84- if i == len ( words ) {
78+ if i == size {
8579 return words
8680 }
8781
88- return append (words [:i ], words [i + 1 :]... )
82+ words [i ] = words [size - 1 ]
83+ return words [:size - 1 ]
8984}
9085
9186func isTransable (a , b string ) bool {
0 commit comments