当前位置:网站首页>【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的order by
- DAP data scheduling function improvement description
- AI quantitative transaction (II) -- tushare financial data framework
- [rust submission] review impl trail and dyn trail in rust
- "Comment positionner l'industrie" dans la planification industrielle locale / parc
- 【LeetCode】22. 括号生成
- (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
- 如何绘制产业招商地图
- [rust contribution] implement Message Oriented Middleware (6) -client from zero
- 2. play the chromatic harmonica
猜你喜欢

数学分析_笔记_第3章:极限

Mstp+vrrp+ospf implements a three-tier architecture

Cesium 加载显示热力图
Zoran community

严重的PHP缺陷可导致QNAP NAS 设备遭RCE攻击

Development of trading system (II) -- market data

Turn 2D photos into 3D models to see NVIDIA's new AI "magic"!

(超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写

How to use ide to automatically sign and debug Hongmeng application

How many images can opencv open?
随机推荐
acmStreamOpen返回值问题
Development of trading system (V) -- Introduction to Sinovel counter
Simple integration of client go gin 11 delete
opencv是开源的吗?
OpenSUSE environment variable settings
Crawl Sina Weibo fans
Jilin University 22 spring March new development English comprehensive course (I) assignment assessment-00080
How to quickly deliver high-value software
General steps for QT compiling database plug-ins
What is the difference between learning code, rolling code and fixed code? The number of repeated codes, coding capacity and the principle of rolling code
[rust contribution] implement Message Oriented Middleware (6) -client from zero
Smart wind power: operation and maintenance of digital twin 3D wind turbine intelligent equipment
Work assessment of pharmacotherapeutics of Jilin University in March of the 22nd spring -00064
web3.eth. Getpendingtransactions() returns null?
Development of trading system (XI) -- Introduction to quickfix
PHP代码审计1—PHP.ini的那些事
Jilin University 22 spring March "career design" assignment assessment-00072
How to use ide to automatically sign and debug Hongmeng application
Mysql的order by
Hello CTP (II) -- Introduction to CTP