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 ...
随机推荐
- luogu P4654 [CEOI2017]Mousetrap
传送门 这里把终点设为根方便后续处理,那么目标就是要让老鼠走到根 首先考虑老鼠动不了的情况,这种情况下可以把从这个点到终点路径上的分支堵住,然后再疏通路径上的走过的边,可以发现这是这种情况下最优的决策 ...
- luogu P4383 [九省联考2018]林克卡特树lct
传送门 题目操作有点奇怪,不过可以发现这就是把树先变成\(k+1\)个连通块,然后每个连通块选一条路径(本题中一个点也是一条路径),然后依次接起来.所以实际上要求的是选出\(k+1\)条点不相交的路径 ...
- ELK-全文检索技术-lucene
ELK : ELK是ElasticSearch,LogStash以及Kibana三个产品的首字母缩写 一.倒排索引 学习elk,必须先掌握倒排索引思想, 参考文档: https://www.cn ...
- vuex的简单理解
初次接触vuex,谈谈我自己的理解.有待后期改进 首先要知道,Vuex 是专门为 Vue.js 设计的状态管理库.我们知道在用vue.js进行前端项目开发时,会出现很多组件相互之间调用属性.状态,小项 ...
- vue动态渲染图片,引用路径需要注意的地方
1.把图片放在和src同级的static里面,这用按照正常的方式进行引入,例如: 2.图片可以在其他文件夹,但是在script引入是必须加上require <img :src="ite ...
- Delphi 语句
- axios 请求常用组件,及其错误
1. var nodeModules = path.join(processPath, 'node_modules') var querystring = require(nodeModules + ...
- AIX系统的备份和恢复
1.AIX备份命令
- Google为远程入侵Titan M芯片提供最高150万美元的赏金
Google最近发布了一项新的公告,旨在提高对发现和报告Android操作系统中的严重漏洞的漏洞赏金的奖励,Google昨天为黑客设定了新的挑战性水平,使他们可以赢得高达150万美元的赏金. 从今天开 ...
- 【NOIP2017模拟12.3】子串
题目 分析 对于当前枚举串 \(now\),从前往后扫.若扫到 \(i\),\(s_i\) 是 ; \(s_j\) 的子串 \((i < j < now)\),我们就可以跳过不匹配 \(i ...