mycode   错误,因为借鉴了Number of Islands问题中的方法,导致在for循环中即使已经出现了答案,也还会继续遍历。但是两个题目的不同时,island需要找出所有的情况,这个题只需要找到第一个完整结果就可以返回

参考:

def exist(board, word):
"""
:type board: List[List[str]]
:type word: str
:rtype: bool
"""
def find(board, word, i, j, m, n):
if word == '':
print('if',i,j)
return True
if i < 0 or i >= m or j < 0 or j >= n:
print('if2',i,j)
return False
elif word[0] == board[i][j]:
print('elif',i,j)
board[i][j] = None #标记
res = find(board, word[1:], i+1, j, m, n)or find(board, word[1:], i-1, j, m, n)or find(board, word[1:], i, j+1, m, n)or find(board, word[1:], i, j-1, m, n)
board[i][j] = word[0] #恢复原来的值
return res
print(i,j)
if len(word) == 0:
return True
m = len(board)
if m == 0:
return False
n = len(board[0])
for i in range(m):
for j in range(n):
if find(board, word, i, j, m, n):
return True
return False
 

leetcode-mid-backtracking -22. Generate Parentheses-79 Word Search -NO的更多相关文章

  1. leetcode个人题解——#22 Generate Parentheses

    思路: 递归解决,如果左括号个数小于右括号或者左括号数小于总括号对数,则生成一个左括号,如果左括号数大于右括号,生成一个右括号. class Solution { public: vector< ...

  2. [Leetcode][Python]22: Generate Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...

  3. 22. Generate Parentheses(ML)

    22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...

  4. 刷题22. Generate Parentheses

    一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...

  5. 刷题79. Word Search

    一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...

  6. 【LeetCode】22. Generate Parentheses (2 solutions)

    Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...

  7. [LeetCode] 79. Word Search 词语搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  8. 【LeetCode】79. Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...

  9. 22. Generate Parentheses (recursion algorithm)

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  10. [LeetCode] 79. Word Search 单词搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

随机推荐

  1. linux php环境搭建

    1.我使用的是一键安装包 下载地址: https://lnmp.org/download.html2.我下载的是完整包 http://soft.vpser.net/lnmp/lnmp1.4-full. ...

  2. vue的v-model指令原理分析

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. iOS和Android图标大小

    iOS版 有关要求和指导原则适用于iOS的应用程序图标的更多详细信息,请参阅的iOS人机界面指南:图标和图像尺寸和技术Q&A QA1686:iPad和iPhone上的应用程序图标. 所有图标都 ...

  4. shell脚本之删除内容相同的重复文件

    #!/bin/bash #!当前文件夹下,删除内容相同的重复文件,只保留重复文件中的一个. ls -lS --time-style=long-iso | awk 'BEGIN{ getline;get ...

  5. Linux系统性能测试工具(九)——文件系统的读写性能测试工具之iozone

    本文介绍关于Linux系统(适用于centos/ubuntu等)的文件系统的读写性能测试工具-iozone: 参考链接: https://www.cnblogs.com/Dev0ps/p/788938 ...

  6. ansible获取远程机器上的ip地址

    问题: 想要使用ansible-playbook脚本创建一个以远程机器的IP命名的目录 错误的代码:# vim test.yml --- - hosts: all remote_user: root ...

  7. Linux课程学习 第四课

    学习必须如蜜蜂一样,采过许多花,这才能酿出蜜来 这月事比较多,每课的笔记都会慢慢补回来的,做事得有始有终 在网络上,人们越来越倾向于传输压缩格式的文件,原因是压缩文件体积小,在网速相同的情况下,传输时 ...

  8. Mysql中event事件的入门

    Mysql中event事件的入门 主要涉及的知识点:mysql的存储过程.mysql的event事件调度. 参考资料: Qiao_Zhi的博客:[周期性执行事件]MySQL事件(Event)& ...

  9. 【CF461B】Appleman and Tree

    题目大意:给定一棵 N 个节点的有根树,1 号节点为根节点,每个点有两种颜色(黑.白),现给出树上每个节点的颜色,求有多少种分割树的方式,使得分割出的每个联通块中有且仅有一个黑点. 题解:树形dp 由 ...

  10. win10激活方法 windows 10 最简单的激活方法

      1.首先,要用管理员权限打开cmd命令行窗口,可以搜索框中输入“cmd”,在出现的“命令行提示符”,图标上右击“以管理员身份运行”.   2.为了让其它激活工具的密钥清除,先卸载密钥,在命令行输入 ...