[leetcode]Word Search @ Python
原题地址:https://oj.leetcode.com/problems/word-search/
题意:
Given a 2D board and a word, find if the word exists in the grid.
The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.
For example,
Given board =
[
["ABCE"],
["SFCS"],
["ADEE"]
]
word = "ABCCED", -> returns true,
word = "SEE", -> returns true,
word = "ABCB", -> returns false.
解题思路:使用dfs来搜索,为了避免已经用到的字母被重复搜索,将已经用到的字母临时替换为'#'就可以了。不知道用bfs可行否。
代码:
class Solution:
# @param board, a list of lists of 1 length string
# @param word, a string
# @return a boolean
def exist(self, board, word):
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
[leetcode]Word Search @ Python的更多相关文章
- [LeetCode] Word Search II 词语搜索之二
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- [LeetCode] Word Search 词语搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- Leetcode: word search
July 6, 2015 Problem statement: Word Search Given a 2D board and a word, find if the word exists in ...
- LeetCode: Word Search 解题报告
Word SearchGiven a 2D board and a word, find if the word exists in the grid. The word can be constru ...
- LeetCode() Word Search II
超时,用了tire也不行,需要再改. class Solution { class TrieNode { public: // Initialize your data structure here. ...
- [leetcode]Word Ladder @ Python
原题地址:https://oj.leetcode.com/problems/word-ladder/ 题意: Given two words (start and end), and a dictio ...
- [leetcode]Word Break @ Python
原题地址:https://oj.leetcode.com/problems/word-break/ 题意: Given a string s and a dictionary of words dic ...
- [Leetcode] word search 单词查询
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- [LeetCode] Word Search [37]
题目 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro ...
随机推荐
- BZOJ4065 : [Cerc2012]Graphic Madness
因为两棵树中间只有k条边,所以这些边一定要用到. 对于每棵树分别考虑: 如果一个点往下连着两个点,那么这个点往上的那条边一定不能用到. 如果一个点往下连着一个点,那么这个点往上的那条边一定不能用到. ...
- Linux——多线程下解决生产消费者模型
我们学习了操作系统,想必对生产消费者问题都不陌生.作为同步互斥问题的一个经典案例,生产消费者模型其实是解决实际问题的基础模型,解决很多的实际问题都会依赖于它.而此模型要解决最大的问题便是同步与互斥.而 ...
- .Net 环境下C# 通过托管C++调用本地C++ Dll文件
综述 : 本文章介绍.Net 环境下C# 通过托管C++调用本地C++ Dll文件, 示例环境为:VS2010, .Net4.0, Win7. 具体事例为测试C++, C#, 及C#调用本地C++D ...
- uva 10154 - Weights and Measures【dp】qi
题意:uva 10154 - Weights and Measures 题意:有一些乌龟有一定的体重和力量,求摞起来的最大高度.力量必须承受其上面包含自己的所有的重量. 分析:先按其能举起来的力量从小 ...
- a标签连接空标签的方法
在写页面时,想把a标签设置成空链接,方便后面数据的连接可以有几种方法. 1. <a herf=""></a> 这种方法会默认打开本页面,重新刷新一次页面. ...
- Embarcadero RAD Studio XE5
英巴卡迪诺 RAD Studio XE是终极应用程序开发套件,能以最快速方式为Windows.Mac OS X. .NET. PHP. Web和移动设备可视化开发数据丰富.界面美观的跨平台应用程序.R ...
- 解决winform中mdi子窗体加载时显示最大化最小化按钮的方法
场景:在mid加载子窗体的时候如果指定WindowState为Maximized,加载完成后主窗体会显示最大化.最小化.关闭的按钮图标. 解决方法: 1.更改主窗体FormMain的属性.制定Main ...
- JavaScript学习总结(十五)——Function类
在JavaScript中,函数其实是对象,每个函数都是Function类的实例,既然函数对象,那么就具有自己的属性和方法,因此,函数名实际上也是一个指向函数对象的指针,不会与某个函数绑定. 一.函数的 ...
- 玩DNF开启NVIDIA独显的方法
管理员身份运行后,点下红圈圈里的X,打开驱动配置文件,在Profiles里输入DNF,打开配置文件 把do not display this profile in the control panel ...
- iOS中使用RegexKitLite来试用正则表达式
转:http://blog.csdn.net/nullcn/article/details/6338592 准备工作,下载RegexKitLite 软件包,解压后有2个文件,需要加载到project中 ...