起床時間:0900
睡太晚,女生月常。我剛剛按到右上鐵人發文,整文章不見
給一個 2D 字元網格 board 與字串 word,判斷是否能在網格中用「上下左右相鄰」的路徑拼出 word。同一格不能重複使用。
寫了一半,感覺好像可以寫出暴力解 for迴圈遍歷整個board 找到目標字就進入檢查function 檢查function要獨立寫出來 這題應該是遞迴?
class Solution: def exist(self, board: List[List[str]], word: str) -> bool: word_idx = 0 while(word_idx < len(world)){ for row in board: for val in row: if val is word[word_idx]: check() if check is true: word_idx += 1 check() } def check(cur: List[int], target: str) -> bool:
整體想法「遍歷整個 board,遇到首字母就進入檢查 function,並用遞迴檢查四個方向」就是這題的典型做法(Backtracking/DFS)
但。
還沒看