[LeetCode]题解(python):079 Word Search
题目来源
https://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.
题意分析
Input:
:type board: List[List[str]]
:type word: str
Output:
:rtype: bool
Conditions:给出一个字符串,看这个字符串是否可以在board中找到,找到的条件是:在board可以连成一条直线,注意已经用过的元素不能再用。
题目思路
用dfs,每次遍历上下左右(注意边界)。避免使用过的元素再次使用,则可以将已经使用过的元素替换为“#”,在使用完之后再替换回来。
AC代码(Python)
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 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 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 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 if y < len(board[x]) - 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[i])):
if board[i][j] == word[0]:
if dfs(i, j, word[1:]):
return True
return False
[LeetCode]题解(python):079 Word Search的更多相关文章
- Java for LeetCode 079 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(79) Word Search
题目 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro ...
- LeetCode题解:(139) Word Break
题目说明 Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, dete ...
- 079 Word Search 单词搜索
给定一个二维面板和一个单词,找出该单词是否存在于网格中.这个词可由顺序相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用.例如,给定 二 ...
- LeetCode题解之 Increasing Order Search Tree
1.题目描述 2/问题分析 利用中序遍历,然后重新构造树. 3.代码 TreeNode* increasingBST(TreeNode* root) { if (root == NULL) retur ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- 212. Word Search II
题目: Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...
- 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...
- [LeetCode 题解] Search in Rotated Sorted Array
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目描述 Suppose an array ...
随机推荐
- BZOJ2388 : 旅行规划
考虑分块,每块维护两个标记$ts,td$. 那么对于块中一个位置$i$,它的实际值为$i\times td+ts+v_i$. 修改的时候,对于整块,直接打标记,对于零散的暴力修改,然后重构凸壳,时间复 ...
- MONO 使用重要提示
重要提示:如果要使用mvc这样的框架(网址没有扩展名或扩展名没有正常规律),请一定用Jexus,而不要用apache/nginx等. ASP.NET跨平台初学者要注意: 1.不要开始就用freeBSD ...
- android开发 BaseAdapter中getView()里的3个参数是什么意思
BaseAdapter适配器里有个getView()需要重写public View getView(int position,View converView,ViewGroup parent){ // ...
- 【C语言】14-返回指针的函数与指向函数的指针
前言 前面我们花了接近3个章节学习指针,应该都感受到指针的强大了吧.指针可以根据地址直接操作内存中的数据,使用得当的话,不仅能使代码量变少,还能优化内存管理.提升程序性能.关于指针的内容还非常多,比如 ...
- 李洪强-C语言5-函数
C语言函数 一.函数 C语言程序是由函数构成的,每个函数负责完成一部分的功能,函数将工恩呢该封装起来,以供程序调用. 二.函数定义 目的:将一些常用的功能封装起来,以供日后调用. 步骤:确定函数名,确 ...
- hello world 驱动程序编写
操作系统课程设计选题 驱动程序的编写和安装. 经过一天多的努力,终于把我的第一个驱动程序模块成功编写并实现插入内核和移除,在这里把过程记录下来方便以后查看,也给其他为之困扰的朋友一个建议. 环境: ...
- RTO & RPO
作者:王文洋链接:https://www.zhihu.com/question/30753842/answer/49334210来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- NOJ 1072 The longest same color grid(尺取)
Problem 1072: The longest same color grid Time Limits: 1000 MS Memory Limits: 65536 KB 64-bit in ...
- Mac 配置环境变量
系统:mac OS 10.10.3 or later 1.如果不能使用一些常用终端命令,如ls,open.可能是环境变量配置不当导致的.尤其是手动修改 输入以下命令: export PATH=/us ...
- jQuery系列:N种方法大总结
jquery自定义属性,区分prop()和attr() jQueryObject.prop( propertyName [, value ] ):为添加,获取属性(property),并非attrib ...