当前位置:网站首页>【LeetCode】22. bracket-generating
【LeetCode】22. bracket-generating
2022-06-25 04:10:00 【LawsonAbs】
1 subject
2 thought
Use deep search
There are two choices at a time , Push , Out of the stack . Then judge whether the string result is reasonable .
n Indicates the logarithm of the production bracket
3 Code
import copy
class Solution:
def generateParenthesis(self, n: int) -> List[str]:
res = []
self.dfs(n,n,res,[])
# print(res)
# tmp = []
# for i in res:
# if self.judge(i):
# tmp.append("".join(i))
return res
# Determine whether stack brackets match
def judge(self,string):
left = 0
right = 0
for s in string:
if s == "(":
left += 1
else:
if left >0: # Description of inventory
left -=1
else:
return False
return True
# left Indicates the number of left parentheses ,right Indicates the number of right parentheses
def dfs(self,left,right,res,tmp):
if left == 0 and right ==0: # It's all over
if self.judge(tmp):
res.append(copy.deepcopy("".join(tmp)))
return
# There are two choices every time , discharge left Let's go right?
if left:
tmp.append("(")
self.dfs(left-1,right,res,tmp)
tmp.pop()
if right:
tmp.append(")")
self.dfs(left,right-1,res,tmp)
tmp.pop()
边栏推荐
- MySQL插入过程报错1062,但是我没有该字段。
- Cesium 加载显示热力图
- Mobile mall project operation
- Windows 2003 64 bit system PHP running error: 1% is not a valid Win32 Application
- Uniapp makes mobile app programs, using uni Choosevideo record video, video playback is fuzzy, and the resolution is low
- 【openwrt】推荐一个国内开发的openwrt的版本,iStoreOS简介,非常好用,主要是做了一些优化。解决了汉化的问题。
- Cesium drag 3D model
- opencv是开源的吗?
- numpy np tips: numpy数组的squeeze等处理
- 数字时代的“文艺复兴”?起底数字藏品,让人欢喜让人愁
猜你喜欢

PHP代码审计1—PHP.ini的那些事

Do you really need automated testing?

1、项目第二阶段——用户注册和登陆

Maybe it's the wrong reason

SEO的5大关键指标:排名+流量+会话+停留时长+跳出率

Crawler crawls Sina Weibo data

NFT Insider #63:The Sandbox与时代杂志达成合作,YGG成立西班牙subDAO

【Harmony OS】【ARK UI】ETS 上下文基本操作
![[team learning] SQL programming language notes - task04](/img/3e/c75f5b4610c6b6700567fe75cd2339.png)
[team learning] SQL programming language notes - task04

(ultra detailed onenet TCP protocol access) arduino+esp8266-01s accesses the Internet of things platform, uploads real-time collected data /tcp transparent transmission (and how to obtain and write Lu
随机推荐
DAP数据调度功能完善说明
Jilin University 22 spring March new development English comprehensive course (I) assignment assessment-00080
数字时代的“文艺复兴”?起底数字藏品,让人欢喜让人愁
PHP code audit 1 - php Ini
Hello CTP (III) - CTP quotation API
Development of trading system (XI) -- Introduction to quickfix
OpenSUSE environment variable settings
Crawler grabs the data of Douban group
La gamme NFT Color, qui représente la diversité, est en ligne sur la plate - forme du marché Sandbox
Jilin University 22 spring March document retrieval assignment assessment-00073
9 necessary soft skills for program ape career development
The 5th series of NFT works of missing parts was launched on the sandbox market platform
学习码 滚动码 固定码 有什么区别重码数,编码容量滚动码的原理
【Proteus仿真】Arduino UNO按键控制数码管闪烁增/减显示
Work assessment of Biopharmaceutics of Jilin University in March of the 22nd spring -00031
BGP biplane architecture
Serious PHP defects can lead to rce attacks on QNAP NAS devices
MySQL插入过程报错1062,但是我没有该字段。
95% 程序员都在这里摸鱼……
Development of trading system (IX) -- dark pool technology