当前位置:网站首页>【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()
边栏推荐
- 【组队学习】SQL编程语言笔记——Task04
- Smart wind power: operation and maintenance of digital twin 3D wind turbine intelligent equipment
- client-go gin的简单整合十-Update
- Crawler crawls Sina Weibo data
- Development of trading system (I) -- Introduction to trading system
- Intel 13代酷睿首次露出真面目:68MB缓存提升明显
- 2.吹响半音阶口琴
- cesium 图形标注圆形、正方形、多边形、椭圆等
- 95% 程序员都在这里摸鱼……
- Russian Airi Research Institute, etc. | SEMA: prediction of antigen B cell conformation characterization using deep transfer learning
猜你喜欢
Development of trading system (VII) -- Analysis of trading delay
长沙“求才”:“下力气”与“出实招”并进,“快发展”和“慢生活”兼得
墨天轮访谈 | IvorySQL王志斌—IvorySQL,一个基于PostgreSQL的兼容Oracle的开源数据库
"Grammar sugar" -- my new programming knowledge
如何使用IDE自动签名调试鸿蒙应用
Russian Airi Research Institute, etc. | SEMA: prediction of antigen B cell conformation characterization using deep transfer learning
[rust submission] review impl trail and dyn trail in rust
冷热酸甜、想成就成?冷酸灵母公司登康口腔欲在深交所主板上市
Mobile mall project operation
[team learning] SQL programming language notes - task04
随机推荐
Jilin University 22 spring March new development English comprehensive course (I) assignment assessment-00080
1. first knowledge of chromatic harmonica
《Missing Parts》NFT 作品集第 5 系列上线 The Sandbox 市场平台
How many images can opencv open?
client-go gin的简单整合十一-Delete
Work assessment of Biopharmaceutics of Jilin University in March of the 22nd spring -00031
9 necessary soft skills for program ape career development
PHP代码审计2—这些函数必知必会
Development of trading system (I) -- Introduction to trading system
地方/園區產業規劃之 “ 如何進行產業定比特 ”
小心被偷脸!天天用的人脸识别风险原来这么多?
Create SQLite table with shell script and add SQL statement -- General
ZABBIX installation pit avoidance Guide
Crawler grabs the data of Douban group
文本关键词提取:ansj
2022-06-21-Flink-49(一. SQL手册)
冷热酸甜、想成就成?冷酸灵母公司登康口腔欲在深交所主板上市
Comprehensive assignment of thesis writing instruction of Dongcai
List rendering in wechat applet
Jilin University 22 spring March "official document writing" assignment assessment-00034