当前位置:网站首页>【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()
边栏推荐
- Is opencv open source?
- @RequestBody解决获取参数为null
- Mstp+vrrp+ospf implements a three-tier architecture
- Trading system development (IV) - trading counter system
- Uniapp makes mobile app programs, using uni Choosevideo record video, video playback is fuzzy, and the resolution is low
- Cesium drag 3D model
- 冷热酸甜、想成就成?冷酸灵母公司登康口腔欲在深交所主板上市
- 警惕超范围采集隐私-移动APP违规十宗罪
- Comprehensive operation of financial risk management X of Dongcai
- Do you really need automated testing?
猜你喜欢

1280_C语言求两个无符号整形的平均值

ZABBIX installation pit avoidance Guide

Development of trading system (VIII) -- Construction of low delay network

Hello CTP (IV) - CTP transaction API

DAP数据调度功能完善说明

Exercise: how difficult is it to simulate the blessing lottery two-color ball - China 500W grand prize? Just run the code.

acmStreamOpen返回值问题

opencv 红色区域在哪里?

代錶多樣性的彩色 NFT 系列上線 The Sandbox 市場平臺

Mobile mall project operation
随机推荐
opencv是开源的吗?
PHP code audit 1 - php Ini
Jilin University 22 spring March "official document writing" assignment assessment-00034
1. first knowledge of chromatic harmonica
数字时代的“文艺复兴”?起底数字藏品,让人欢喜让人愁
[proteus simulation] Arduino uno key controls the flashing increase / decrease display of nixie tube
Development of trading system (VIII) -- Construction of low delay network
The problem that only the home page can be accessed under openSUSE Apache laravel
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
Flutter FittedBox组件
《Missing Parts》NFT 作品集第 5 系列上线 The Sandbox 市场平台
墨天轮访谈 | IvorySQL王志斌—IvorySQL,一个基于PostgreSQL的兼容Oracle的开源数据库
How to install opencv? Opencv download installation tutorial
BSC parsing input data of transaction
升级cmake
PHP代码审计2—这些函数必知必会
【openwrt】推荐一个国内开发的openwrt的版本,iStoreOS简介,非常好用,主要是做了一些优化。解决了汉化的问题。
About PLSQL error initialization failure
如何使用IDE自动签名调试鸿蒙应用
[harmony OS] [ark UI] basic ETS context operations