题目来源


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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. LeetCode题解:(139) Word Break

    题目说明 Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, dete ...

  4. 079 Word Search 单词搜索

    给定一个二维面板和一个单词,找出该单词是否存在于网格中.这个词可由顺序相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用.例如,给定 二 ...

  5. LeetCode题解之 Increasing Order Search Tree

    1.题目描述 2/问题分析 利用中序遍历,然后重新构造树. 3.代码 TreeNode* increasingBST(TreeNode* root) { if (root == NULL) retur ...

  6. 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 ...

  7. 212. Word Search II

    题目: Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...

  8. 【LeetCode】211. Add and Search Word - Data structure design 添加与搜索单词 - 数据结构设计

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:Leetcode, 力扣,211,搜索单词,前缀树,字典树 ...

  9. [LeetCode 题解] Search in Rotated Sorted Array

    前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目描述 Suppose an array ...

随机推荐

  1. HangOver

    HangOver Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Su ...

  2. 亲和数[HDU2040]

    亲和数 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  3. BZOJ3739 : DZY loves math VIII

    显然当且仅当$\gcd(i,j)=1$时才对答案有贡献,化简得 \[\begin{eqnarray*}ans&=&\sum_{i=1}^n\sum_{j=1}^i\mu(ij)[\gc ...

  4. Cobar_基于MySQL的分布式数据库服务中间件

    Cobar是阿里巴巴研发的关系型数据的分布式处理系统,是提供关系型数据库(MySQL)分布式服务的中间件,该产品成功替代了原先基于Oracle的数据存储方案,它可以让传统的数据库得到良好的线性扩展,并 ...

  5. linux-centos下源代码安装subversion (svn)

    1.svn的源代码 1.1 可以在官方下载,官方地址 :svn 1.6.17源码包  http://subversion.tigris.org/servlets/ProjectDocumentList ...

  6. 使用STL库sort函数对vector进行排序

    使用STL库sort函数对vector进行排序,vector的内容为对象的指针,而不是对象. 代码如下 #include <stdio.h> #include <vector> ...

  7. Hibernate工作原理及为什么要用?

    Hibernate工作原理及为什么要用? 原理:1.通过Configuration().configure();读取并解析hibernate.cfg.xml配置文件2.由hibernate.cfg.x ...

  8. highcharts报表插件之expoting参数的使用

    exporting 参数配置 本文转载自:http://blog.csdn.net/myjlvzlp/article/details/8531275 说明:导出及打印选项 打印导出功能的配置项. 1. ...

  9. 推荐给开发者的20个优秀PHP框架

    推荐给开发者的20个优秀PHP框架 来源:developerslane   时间:2015-01-13 19:48:06   阅读数:111916 分享到:14 [导读] PHP是非常受欢迎并且很有影 ...

  10. RT-Thread相同优先级线程的调度

    /* 静态线程的 线程堆栈*/ ]; ]; /* 静态线程的 线程控制块 */ static struct rt_thread thread_test1; static struct rt_threa ...