当前位置:网站首页>【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()
边栏推荐
- How to use crawlers to capture bullet screen and comment data of station B?
- 2022-06-21-Flink-49(一. SQL手册)
- client-go gin的简单整合十一-Delete
- opencv最大能打开多少图像?
- Create SQLite table with shell script and add SQL statement -- General
- Development of trading system (x) -- fix agreement
- 【Harmony OS】【ArkUI】ets开发 图形与动画绘制
- Although the Internet in the traditional sense has long ceased to exist, this does not mean that the Internet has long disappeared
- AI quantitative transaction (I) -- Introduction to quantitative transaction
- [harmony OS] [arkui] ETS development graphics and animation drawing
猜你喜欢

How to use ide to automatically sign and debug Hongmeng application

Hello CTP (III) - CTP quotation API

Changsha's "talent seeking": "making efforts" and "making practical moves" go hand in hand, "rapid development" and "slow life" go hand in hand

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

DAP数据调度功能完善说明

冷热酸甜、想成就成?冷酸灵母公司登康口腔欲在深交所主板上市

1. first knowledge of chromatic harmonica

numpy np tips:使用opencv对数组插值放缩到固定形状 cv2.resize(res, dsize=(64, 64), interpolation=cv2.INTER_CUBIC)

Flutter FittedBox组件

Crawler crawls Sina Weibo data
随机推荐
2022-06-21-Flink-49(一. SQL手册)
Turn 2D photos into 3D models to see NVIDIA's new AI "magic"!
[proteus simulation] Arduino uno key controls the flashing increase / decrease display of nixie tube
Hot and cold, sweet and sour, want to achieve success? Dengkang oral, the parent company of lengsuanling, intends to be listed on the main board of Shenzhen Stock Exchange
【LeetCode】148. 排序链表
Russian Airi Research Institute, etc. | SEMA: prediction of antigen B cell conformation characterization using deep transfer learning
墨天轮访谈 | IvorySQL王志斌—IvorySQL,一个基于PostgreSQL的兼容Oracle的开源数据库
Simple integration of client go gin 11 delete
Understand (DI) dependency injection in PHP
windows 2003 64位系统php运行报错:1% 不是有效的 win32 应用程序
Cesium graphic annotation circle, square, polygon, ellipse, etc
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
(超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写
Development of trading system (XII) - Official quickfix document
Cesium loading display thermal diagram
"Grammar sugar" -- my new programming knowledge
智慧风电:数字孪生 3D 风机智能设备运维
[team learning] SQL programming language notes - task04
Jilin University 22 spring March document retrieval assignment assessment-00073
La gamme NFT Color, qui représente la diversité, est en ligne sur la plate - forme du marché Sandbox