leetcode-mid-backtracking -22. Generate Parentheses-79 Word Search -NO
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的更多相关文章
- leetcode个人题解——#22 Generate Parentheses
思路: 递归解决,如果左括号个数小于右括号或者左括号数小于总括号对数,则生成一个左括号,如果左括号数大于右括号,生成一个右括号. class Solution { public: vector< ...
- [Leetcode][Python]22: Generate Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 22: Generate Parentheseshttps://oj.leet ...
- 22. Generate Parentheses(ML)
22. Generate Parentheses . Generate Parentheses Given n pairs of parentheses, write a function to ge ...
- 刷题22. Generate Parentheses
一.题目说明 这个题目是22. Generate Parentheses,简单来说,输入一个数字n,输出n对匹配的小括号. 简单考虑了一下,n=0,输出"";n=1,输出" ...
- 刷题79. Word Search
一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...
- 【LeetCode】22. Generate Parentheses (2 solutions)
Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...
- [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 ...
- 【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 ...
- 22. Generate Parentheses (recursion algorithm)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [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 ...
随机推荐
- centos安装mysql以及授权登录用户
CentOS第一次安装MySQL的完整步骤 目录 1.官方安装文档 2.下载 Mysql yum包 3.安转软件源 4.安装mysql服务端 5.首先启动mysql ...
- wex5 如何在js中给data添加数据
var options = { defaultValues :[ {'xuetang' : xuetang,'time' : time} ] }; this.comp("xuetangDat ...
- vue学习【四】vuex快速入门
大家好,我是一叶,今天我们继续踩坑.今天的内容是vuex快速入门,页面传值不多的话,不建议vuex,直接props进行父子间传值就行,使用vuex就显得比较臃肿. 我们先预览一下效果,如图1所示. 图 ...
- 使用html2canvas实现屏幕截图
相关文件(vue3.0) <script src="https://cdn.jsdelivr.net/bluebird/latest/bluebird.js">< ...
- Flask开发系列之Web表单
Flask开发系列之Web表单 简单示例 from flask import Flask, request, render_template app = Flask(__name__) @app.ro ...
- mariadb主从架构
mariadb主从架构(异步)和集群 一般应用的场所是网站,主的机器是可以写可以读,从的机器可以读,也可以写,但不会同步.只有主的机器增删改,从的机器才会同步. 主从至少三个线程:dump.I/O t ...
- linux开启Rsyslog服务收集日志
一.查看是否安装了rsyslog服务 [root@server- ~]# yum install -y rsyslog 已加载插件:fastestmirror Loading mirror speed ...
- 如何替换B字段内包含A字段的那部分内容
Customer表A字段 varchar(50) 内容(客户姓名)B字段 varchar(1000) 内容(其他字符...客户姓名...其他字符)需要达到效果:将B字段中的客户姓名替换 ...
- feign请求写法
@FeignClient(value = "test", url = "${proxy.srvs.test:}") public interface ISubS ...
- Mysql定时器定时删除表数据
由于测试环境有张日志表没定时2分钟程序就狂插数据,导致不到1一个月时间,这张日志表就占用了6.7G的空间,但是日志刷新较快,有些日志就没什么作用,就写了个定时器,定期删除这张表的数据 首先先查看mys ...