当前位置:网站首页>【LeetCode】22. 括号生成
【LeetCode】22. 括号生成
2022-06-25 03:53:00 【LawsonAbs】
1 题目
2 思想
使用深搜
每次有两种选择,入栈,出栈。然后判断得到的字符串结果是否合理即可。
n表示生产括号的对数
3 代码
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
# 判断栈括号是否匹配
def judge(self,string):
left = 0
right = 0
for s in string:
if s == "(":
left += 1
else:
if left >0: # 说明有存货
left -=1
else:
return False
return True
# left 表示左括号个数,right表示右括号个数
def dfs(self,left,right,res,tmp):
if left == 0 and right ==0: # 全部放完了
if self.judge(tmp):
res.append(copy.deepcopy("".join(tmp)))
return
# 每次都有两种选择,放left还是放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()
边栏推荐
- 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
- 【LeetCode】148. 排序链表
- Jilin University 22 spring March "automatic control principle" work assessment-00050
- Russian Airi Research Institute, etc. | SEMA: prediction of antigen B cell conformation characterization using deep transfer learning
- 【Rust投稿】从零实现消息中间件(6)-CLIENT
- OpenSUSE installation pit log
- BGP biplane architecture
- 如何使用IDE自动签名调试鸿蒙应用
- Xidian AI ranked higher than Qingbei in terms of AI majors, and Nantah ranked the first in China in 2022 in terms of soft science majors
- Development of trading system (XII) - Official quickfix document
猜你喜欢

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

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

【Harmony OS】【ARK UI】ETS 上下文基本操作

Russian Airi Research Institute, etc. | SEMA: prediction of antigen B cell conformation characterization using deep transfer learning

MySQL modifies and deletes tables in batches according to the table prefix

The 5th series of NFT works of missing parts was launched on the sandbox market platform

opencv是开源的吗?

【Harmony OS】【ArkUI】ets开发 图形与动画绘制

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

opencv 红色区域在哪里?
随机推荐
2022-06-21-Flink-49(一. SQL手册)
Work assessment of pharmacotherapeutics of Jilin University in March of the 22nd spring -00064
Windows 2003 64 bit system PHP running error: 1% is not a valid Win32 Application
【Harmony OS】【ArkUI】ets开发 图形与动画绘制
opencv是开源的吗?
OpenSUSE environment PHP connection Oracle
DAP数据调度功能完善说明
严重的PHP缺陷可导致QNAP NAS 设备遭RCE攻击
Monitoring pancakeswap new token
BSC parsing input data of transaction
Dr. Sun Jian was commemorated at the CVPR conference. The best student thesis was awarded to Tongji Ali. Lifeifei won the huangxutao Memorial Award
【组队学习】SQL编程语言笔记——Task04
Understand (DI) dependency injection in PHP
opencv最大能打开多少图像?
“语法糖”——我的编程新知
OpenSUSE installation pit log
(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
Trading system development (IV) - trading counter system
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
Jilin University 22 spring March "official document writing" assignment assessment-00029