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 ...
随机推荐
- jq each遍历数组或对象
var arr = ["北京","上海","天津","重庆","河北","河南" ...
- oracle获取年、月、日
--获取年 select extract(year from date'2011-05-17') year from dual; --获取月 select extract(month from dat ...
- Juery入门2
1.Jquery操作文档 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&q ...
- SpringBoot01——Framework Introduced and Helloworld
1.介绍 SpringBoot主要解决的是在微服务的架构下简化配置(有快速配置).前后端分离.快速开发 优点: l 提供了快速启动入门 l 开箱即用.提供默认配置 l 内嵌容器化web项目 l 没有冗 ...
- Rootkit XSS
0x00 XSS Rootkit介绍 Rootkit概念: 一种特殊的恶意软件 类型: 常见为木马.后门等 特点: 隐蔽 持久控制 谈到XSS,一般都是想到 ...
- CreateRemoteThread
CreateRemoteThread是一个Windows API函数,它能够创建一个在其它进程地址空间中运行的线程(也称:创建远程线程)..
- linux基础命令--lsof
lsof(list open files)作用: 是一个列出当前系统打开文件的工具. 注: 在终端下输入lsof即可显示系统打开的文件,因为 lsof 需要访问核心内存和各种文件,所以必须以 root ...
- 【HDU5289】Assignment
题目大意:给定一个长度为 N 的序列,求序列中最大值和最小值相差小于 K 的连续段的个数. 题解: 最大值和最小值相差不超过 K 是一个在值域角度的限制,应考虑采用平衡树或权值...数据结构进行维护. ...
- 指数家族-Beta分布
2. Beta分布 2.1 Beta分布 我们将由几个问题来得引出几个分布: 问题一:1: 2:把这个 个随机变量排序后得到顺序统计量 3:问 是什么分布 首先我们尝试计算 落在一个区间 ...
- jq实现表格多行列复制
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>&l ...