[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 ...
随机推荐
- BZOJ4123 : [Baltic2015]Hacker
黑掉的一定是一个长度为$\lfloor\frac{n+1}{2}\rfloor$的区间. 于是枚举初始点,然后查询包含它的区间的最小值. 通过维护前后缀最小值+单调队列$O(n)$解决. #inclu ...
- redis运用连接池报错解决
redis使用连接池报错解决redis使用十几小时就一直报异常 redis.clients.jedis.exceptions.JedisConnectionException: Could not g ...
- 利用Python的三元表达式解决Odoo中工资条中城镇、农村保险的问题
Python中没有像C#中有三元表达式 A?B:C 但在python中可以通过 A if condition else B 的方式来达到同样的效果. 例如 : 1 if True else 0 输出 ...
- top指令
top - :: up :, user, load average: 0.00, 0.01, 0.05 Tasks: total, running, sleeping, stopped, zombie ...
- 【新产品发布】EVC9001 USB 隔离器
一. 简介 EVC9001采用Analog Device 公司的基于芯片级变压器的iCoupler 磁耦合隔离方案,完成了对USB接口双向隔离功能,隔离电压达 2500V(隔离电源模块 3000V隔 ...
- ASP.NET MVC系列 框架搭建(二)之仓储层的优化
大神勿喷,小神默默学. 会了就是不值一提的东西,不会就是绝对的高大上. 最后上传源码.希望能给读者带来一些新的认识及知识. 还没上过头条..各位大神,请点支持一下小弟. 陆续更新.更新到你会为止!! ...
- Javascript 笔记与总结(1-1)作用域
以语言的角度学习 Js 的底层原理(与 DOM 无关):① 作用域链 ② 词法分析 ③ 闭包 ④ 面向对象(原型链) ① 作用域链 例1 <script> var c = 5; funct ...
- li标签之间的空隙问题(转)
原文地址:http://www.cnblogs.com/laogao/archive/2012/08/13/2636419.html css li 空隙问题 IE6/7的Bug:纵向排列的li中加 ...
- 【转载】loadrunner使用system()函数调用Tesseract-OCR识别验证码遇到的问题
俗话说前人栽树,后人乘凉,此话一点不假,结合云层的一遍文章:http://bbs.51testing.com/thread-533920-1-1.html,知道还有一个Tesseract-OCR可以用 ...
- mysql的事务和select...for update
一.mysql的事务mysql的事务有两种方式:1.SET AUTOCOMMIT=0;也就是关闭了自动提交,那么任何commit或rollback语句都可以触发事务提交;如果SET AUTOCOMMI ...