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. oracle函数nvl,nvl2的区别,nullif函数,coalesce函数

    在oracle中用nvl和nvl2函数来解决为空的情况,例如,如果奖金为空,则为它指定一个数.也就是nvl(奖金字段,指定的奖金),但是两个的类型要一致. 1)nvl()函数 SQL> sele ...

  2. 4种vue当中的指令和它的用法

    1.v-if:判断是否隐藏 2.v-for:数据循环 3.v-bind:class:绑定一个属性 4.v-model:实现数据双向绑定 这里重点说明一个v-if和v-show的区别: 共同点:都是通过 ...

  3. 剑指offer-1:旋转数组的最小数字

    旋转数组的最小数字1.题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4,5,1,2}为{1,2 ...

  4. php学习第一天(记录注意事项)

  5. 2019-11-29-dotnet-core-输出调试信息到-DebugView-软件

    title author date CreateTime categories dotnet core 输出调试信息到 DebugView 软件 lindexi 2019-11-29 10:14:3 ...

  6. Laravel配置全局公共函数的方法步骤

    前言 在laravel项目开发中,经常使用到公共函数,那如何在laravel配置全局公共函数呢??下面话不多说了,来一起看看详细的介绍吧 方法如下 在Laravel项目中我们常常需要定义一些全局的公共 ...

  7. C++ 一周刷完C++基础课程(同C程序进行比较)

    **参考bilibili视频av29504365** ### 一段简单的程序Hello World```#include <iostream>using namespace std;int ...

  8. Hadoop 开发环境虚拟机搭建

    软件下载: VMware软件: 链接:https://pan.baidu.com/s/1gWinLJpfWdAQ8AyEkZxpfg 密码:i2ap 下载Ubuntu 镜像文件; 链接:https:/ ...

  9. 《Head First 软件开发》阅读三

    足够好的设计:以良好的设计完成工作 良好的设计有助于交付软件. 有些项目的进程会打破单一责任的原则,当每个对象只有一个理由去改变时,已经正确地实施了单一责任原则.辨别设计中的多重责任,对实现类中的东西 ...

  10. 如何提升scrapy爬取数据的效率

    在配置文件中修改相关参数: 增加并发 默认的scrapy开启的并发线程为32个,可以适当的进行增加,再配置文件中修改CONCURRENT_REQUESTS = 100值为100,并发设置成了为100. ...