题目来源


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. BZOJ3290 : Theresa与数据结构

    CANCEL操作可以看作删点,X坐标可以离散化 将询问按Z坐标差分,转化成两个求Z<=某个数的和的询问 将操作CDQ分治 每次将前一半的修改.后一半的查询按照Z坐标排序 然后扫描线,每到一个询问 ...

  2. HDU 2612 (BFS搜索+多终点)

    题目链接: http://poj.org/problem?id=1947 题目大意:两人选择图中一个kfc约会.问两人到达时间之和的最小值. 解题思路: 对于一个KFC,两人的BFS目标必须一致. 于 ...

  3. 【Vijos】1792 摆花

    题目链接:https://vijos.org/p/1792 算法:DP 看到这题真的一点不会...只能爆搜一下..但太太慢了..看了题解后,听说是分组背包??不知道.. 好吧,,还是百度了下题解,渐渐 ...

  4. CentOS下搭建使用gitlab 和tortoiseGit使用

    gitlab和github 一样很爽的一个东西 关于gitlab在CentOS下的安装方法地址参考: https://github.com/gitlabhq/gitlab-recipes/tree/m ...

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

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

  6. MySQL 记录不存在时插入 记录存在则更新的实现方法

    INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1; INSERT 中 ON DUPLICATE KEY UP ...

  7. 在Linux中安装SEP Client

    1. 下载Symantec_Endpoint_Protection_12.1.5_Linux_Client_EN, 解压其中的sep-deb.zip   2. 若直接sudo ./install.sh ...

  8. Java类型相互转换byte[]类型,blob类型

    在我们的程序开发当中,经常会用到java.sql.Blob.byte[].InputStream之间的相互转换,但在JDK的API当中,又没有直接给我们提供可用的API,下面的程序片段主要就是实现它们 ...

  9. FastDFS常见命令

    1: 启动FastDFS tracker: ./fdfs_trackered  .../tracker.conf storage: ./fdfs_storaged  .../storage.conf ...

  10. GDC 2016 神秘海域4中使用Substance制作Texture

    TEXTURING UNCHARTED 4: A MATTER OF SUBSTANCE 原文链接 http://www.dualshockers.com/2016/03/16/amazing-unc ...