力扣 542,127 | 青训营笔记

90 阅读3分钟

这是我参与「第四届青训营」笔记创作活动的第6天

叮 听说笔记活动不一定写安卓(

今天做了两道Graph : )

542. 01 Matrix:

- Medium 题

方法BFS, 整体大致的思路是:

先遍历一遍矩阵,如果当前位置是0,把它加到Queue里,如果是1我们先把转成-1,表示当前格子还没有visit过。

进入while queue循环,先对于所有的0,他们去上下左右看,得到(ni,nj),如果(ni,nj) 有出界,直接continue。如果没有出界,且当前格子为-1,我们就让把(ni,nj)加到queue里,同时更新matrix当前格子为step+1~最后直接返回matrix就好啦!

from collections import deque
class Solution:
    def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]:
        row,col=len(mat),len(mat[0])
        queue=deque()
        for i in range(row):
            for j in range(col):
                if mat[i][j]==0:
                    queue.append((i,j))
                else:
                    mat[i][j]=-1 #not visited
        step=0
        while queue:
            for _ in range(len(queue)):
                i,j=queue.popleft() 
                #print(i,j)
                for x,y in [(0,1),(0,-1),(1,0),(-1,0)]:
                    ni=i+x
                    nj=j+y
                    if ni not in range(row) or nj not in range(col):
                        continue
                    if mat[ni][nj]==-1:
                        queue.append((ni,nj))
                        mat[ni][nj]=step+1
            step+=1
        return mat

127. Word Ladder

- Hard 题

大致的思路:

先把beginword放到queue和visited里,把它pop出来。

比如先放hit,pop出hit,如果hit已经是endword了,那就直接return step就好了。

如果hit不是endword那我们就要调用nextword这个函数:

遍历每一个char 然后每一个char 用26个字母给试一下,比如hit->ait/bit/cit; hit->hat/hbt/hct... 每变换一次 我们去看当前变换的词在不在wordlist里,如果在!那太棒啦!append到一个list里。

那这个hit,通过nextWord函数,最后应该是返回[hot]

对于返回出来的这个list 我们再遍历有没有visited过,没有visited过append到queue里!

最后小trick!

因为有的case wordlist很长很长很长很长

如果一开始不写wordList=set(wordList), 那每一次 x in list的查找都是O(n) 会超时555555

但是如果一开始把list转成set 这样每次查找就是O(1) 就能跑通啦。

但好像这个方法不是最优解,最优的方法是双向BFS 我还不太会写呜呜呜呜呜(。•́^•̀。)

还有!!

循环外是return 0,因为有可能出现根本没法从beginword到endword的情况!!

(我一开始就return step 失足少女.jpg)

from collections import deque
class Solution:
    def ladderLength(self, beginWord: str, endWord: str, wordList: List[str]) -> int:
        #要把wordList转成set的 因为后续x in set查找是O(1), 而list查找是O(n)
        wordList=set(wordList)
        visited=set([beginWord])
        queue=deque([beginWord])
        step=0
        while queue:
            for _ in range(len(queue)):
                curWord=queue.popleft()
                if curWord==endWord:
                    step=step+1
                    return step
                newList=self.nextWord(curWord,wordList)
                for word in newList:
                    if word not in visited:
                        queue.append(word)
                        visited.add(word)
                #print(queue)
            step+=1
        #return 0 表示最后没有找到要不然就返回上面的step了
        return 0
                
    def nextWord(self,curWord,wordList):
        newList=[]
        for i in range(len(curWord)):
            #26个字母 一个一个去看可以变换成什么
            for char in range(26):
                newWord=curWord[:i]+chr(ord('a')+char)+curWord[i+1:]
                if newWord!=curWord and newWord in wordList:
                    newList.append(newWord)
        return newList