LeetCode 79 Word Search (Python详解及实现)
2017-08-08 15:30
417 查看
【题目】
Given a 2D board and a word, find if theword exists in the grid.
The word can be constructed from letters ofsequentially adjacent cell, where "adjacent" cells are thosehorizontally or vertically neighboring. The same letter cell may not be usedmore than once.
For example,
Given board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
word = "ABCCED", -> returnstrue,
word = "SEE", -> returns true,
word = "ABCB", -> returnsfalse.
在给定的字母板上寻找给定的单词,单词要用相邻单元重建,相邻意味着从一个字母开始垂直或者水平方向上开始寻找,同一个位置的字母只能访问一次
【思路】深度优先搜索dfs:
首先找到第一个字母,然后进行深度搜索,已经走过的位置不能再次访问
【Python实现】
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 8 14:20:45 2017
@author: Administrator
"""
class Solution(object):
def exist(self, board, word):
"""
:type board: List[List[str]]
:type word: str
:rtype: bool
"""
def dfs(x, y, word):
if len(word) == 0:#说明所有字母均在单词板上找到
return True
#up
if x > 0 and board[x-1][y] == word[0]:
tmp = board[x][y]
board[x][y] = '#'
if dfs(x-1, y, word[1:]):
return True
board[x][y] = tmp
#down
if x < len(board) - 1 and board[x+1][y] == word[0]:
tmp = board[x][y]
board[x][y] = '#'
if dfs(x+1, y, word[1:]):
return True
board[x][y] = tmp
#left
if y > 0 and board[x][y-1] == word[0]:
tmp = board[x][y]
board[x][y] = '#'
if dfs(x, y-1, word[1:]):
return True
board[x][y] = tmp
#right
if y < len(board[0])-1 and board[x][y+1] == word[0]:
tmp = board[x][y]
board[x][y] = '#'
if dfs(x, y+1, word[1:]):
return True
board[x][y] = tmp
return False
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j]==word[0]:#首字母相等的位置
if(dfs(i, j, word[1:])):#寻找下一个字母是否在单词板
return True
return False
if __name__ == '__main__':
board =[['A','B','C','E'],['S','F','C','S'],['A','D','E','E']]
word = "ABCCED"
S = Solution()
S.exist(board, word)
Given a 2D board and a word, find if theword exists in the grid.
The word can be constructed from letters ofsequentially adjacent cell, where "adjacent" cells are thosehorizontally or vertically neighboring. The same letter cell may not be usedmore than once.
For example,
Given board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
]
word = "ABCCED", -> returnstrue,
word = "SEE", -> returns true,
word = "ABCB", -> returnsfalse.
在给定的字母板上寻找给定的单词,单词要用相邻单元重建,相邻意味着从一个字母开始垂直或者水平方向上开始寻找,同一个位置的字母只能访问一次
【思路】深度优先搜索dfs:
首先找到第一个字母,然后进行深度搜索,已经走过的位置不能再次访问
【Python实现】
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 8 14:20:45 2017
@author: Administrator
"""
class Solution(object):
def exist(self, board, word):
"""
:type board: List[List[str]]
:type word: str
:rtype: bool
"""
def dfs(x, y, word):
if len(word) == 0:#说明所有字母均在单词板上找到
return True
#up
if x > 0 and board[x-1][y] == word[0]:
tmp = board[x][y]
board[x][y] = '#'
if dfs(x-1, y, word[1:]):
return True
board[x][y] = tmp
#down
if x < len(board) - 1 and board[x+1][y] == word[0]:
tmp = board[x][y]
board[x][y] = '#'
if dfs(x+1, y, word[1:]):
return True
board[x][y] = tmp
#left
if y > 0 and board[x][y-1] == word[0]:
tmp = board[x][y]
board[x][y] = '#'
if dfs(x, y-1, word[1:]):
return True
board[x][y] = tmp
#right
if y < len(board[0])-1 and board[x][y+1] == word[0]:
tmp = board[x][y]
board[x][y] = '#'
if dfs(x, y+1, word[1:]):
return True
board[x][y] = tmp
return False
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j]==word[0]:#首字母相等的位置
if(dfs(i, j, word[1:])):#寻找下一个字母是否在单词板
return True
return False
if __name__ == '__main__':
board =[['A','B','C','E'],['S','F','C','S'],['A','D','E','E']]
word = "ABCCED"
S = Solution()
S.exist(board, word)
相关文章推荐
- LeetCode 66 Plus One (Python详解及实现)
- LeetCode 55 Jump Game(Python 实现及详解)
- LeetCode 75 SortColor(Python详解及实现)
- LeetCode 73 Set Matrix Zeroes(Python详解及实现)
- LeetCode 80 Remove Duplicates from Sorted Array II (Python详解及实现)
- LeetCode 68 Text Justification(Python详解及实现)
- LeetCode 61. Rotate List(Python详解及实现)
- LeetCode 85 Maximal Rectangle (Python详解及实现)
- LeetCode 90 SubsetsII (Python详解及实现)
- LeetCode 77 Combinations (Python详解及实现)
- LeetCode 91 Decode Ways (Python详解及实现)
- [Leetcode]@python 79. Word Search
- LeetCode 86 Partition List (Python详解及实现)
- LeetCode 81 Search in Rotated Sorted Array II (Python详解及实现)
- LeetCode 72 Edit Distance(Python详解及实现)
- LeetCode 87 Scramble String (Python详解及实现)
- LeetCode 64 Minimum Path Sum(Python详解及代码实现)
- 25_LeetCode 82&83. Remove Duplicates from Sorted List(II) (Python详解及实现)
- LeetCode 65 ValidNumer(Python详解及实现)
- LeetCode 74 Search a 2D Matrix(Python详解及实现)