题目来源


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. 不容易系列之(4)——考新郎[HDU2049]

    不容易系列之(4)——考新郎 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  2. POJ 1743 (后缀数组+不重叠最长重复子串)

    题目链接: http://poj.org/problem?id=1743 题目大意:楼教主の男人八题orz.一篇钢琴谱,每个旋律的值都在1~88以内.琴谱的某段会变调,也就是说某段的数可以加减一个旋律 ...

  3. Win 8 App开发框架解析

    开发前准备: Windows 8 RTM MSDN订阅用户下载地址: https://msdn.microsoft.com/zh-cn/subscriptions/securedownloads/hh ...

  4. 小结:kmp

    复杂度: O(len(a)+len(b)) 技巧及注意: 在匹配的时候记住先要自身匹配然后再匹配即可,同时边界问题不能忽略,处理好点吧. #include <cstdio> #includ ...

  5. POJ 1691 Painting A Board(迭代深搜)

    题目链接 调了一上午,单步的效率太低了,特别是在有递归的情况下...下午来了,输出调试了下,就发现bug了,各种混乱啊. 比较高兴的事,1Y了.本来还准备用edge1优化一下的,结果完全没用到.. # ...

  6. Mysql_mysql多个TimeStamp设置

    timestamp设置默认值是Default CURRENT_TIMESTAMP timestamp设置随着表变化而自动更新是ON UPDATE CURRENT_TIMESTAMP 但是由于 一个表中 ...

  7. iPads和iPhones的Media Queries

    iPad Media Queries 1.iPad Media Queries (所有版本,包括iPad mini) iPads从第一代到至今,总共有五代,也就是iPad1~iPad5,以及Mini ...

  8. ps命令交叉编译

    busybox中的ps命令是针对于嵌入式的,其中一些选项并不完整.因此需要将源码下载下来,进行交叉编译 官方下载地址 github下载地址 含有configure,我在此使用的是这个源码包,官方的包在 ...

  9. hlg 2130 状压dp

    基本的状压dp 需要注意的是两点之间直线最短 所以不需要进行floyd 由于把dp的memset放在了初始化0的后面de了好久的bug.. #include<stdio.h> #inclu ...

  10. Nginx 配置 Basic 认证

    /* * 环境:LNMP(CentOS 6.6 + Nginx 1.8.0) */ 在 Nginx 下配置 Basic 认证需要依靠 Nginx 的 http_auth_basic_module 模块 ...