[leetcode]22. Generate Parentheses生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
For example, given n = 3, a solution set is:
- [
- "((()))",
- "(()())",
- "(())()",
- "()(())",
- "()()()"
- ]
题意: 给定一个长度,生成所有此长度的合法括号序列。
Solution1:Backtracking.
We can observe that two constraints:
(1) left Parentheses should come faster ahead to reach given n.
(2) in each level, we increment left Parentheses count (or right Parentheses count) from given n, and add '(' (or ')') into path
At last, left and right Parentheses count become 0, we can gerenate a result path.
code:
- /*
- Time: O(2^n).
- Space: O(n). use O(n) space to store the sequence(path)
- */
- class Solution {
- public List<String> generateParenthesis(int n) {
- List<String> result = new ArrayList<>();
- if (n == 0) return result;
- helper("", n, n, result);
- return result;
- }
- private void helper(String path, int left, int right, List<String> result) {
- // corner
- if (left > right) return;
- // base
- if (left == 0 && right == 0) {
- result.add(path);
- return;
- }
- if (left > 0) {
- helper(path + "(", left - 1, right, result);
- }
- if (right > 0) {
- helper(path + ")", left, right - 1, result);
- }
- }
- }
[leetcode]22. Generate Parentheses生成括号的更多相关文章
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [CareerCup] 9.6 Generate Parentheses 生成括号
9.6 Implement an algorithm to print all valid (e.g., properly opened and closed) combinations of n-p ...
- 22.Generate Parentheses[M]括号生成
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- generate parentheses(生成括号)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LintCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode]22. Generate Parentheses括号生成
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- 22. Generate Parentheses生成指定个括号
生成指定个数的括号,这些括号可以相互包括,但是一对括号的格式不能乱(就是配对的一个括号的左括号要在左边,右括号要在右边) 思维就是从头递归的添加,弄清楚什么时候要添加左括号,什么时候添加右括号 有点像 ...
- LeetCode 22 Generate Parentheses(找到所有匹配的括号组合)
题目链接 : https://leetcode.com/problems/generate-parentheses/?tab=Description 给一个整数n,找到所有合法的 () pairs ...
随机推荐
- java设计模式--UML类图
2016-06-07 22:46:16 下面简单介绍UML类图:(图是截取的,大家可以用UML工具去画) 1.描述类的类图 类:Person 属性:name age sex 访问权限:- 表 ...
- 记一次nginx403错误
同事开发微信小程序,小程序通过API接口调用我们的人脸比对API,但是一直是提示403,通过查看查看nginx日志,发现请求并没有转发出去,转发出去的请求,应该是301,重定向, 然后就开始在ngin ...
- anaconda 在内网中代理配置
修改anaconda的配置文件,位置在c:\User(或“用户”)\current_user(当前用户)\.condarc,将以下内容拷贝进去, 替换原有内容, 修改 http://proxy.you ...
- jekins构建触发器详解-日程表的使用
日程表参数解释如下: 第一个参数代表的是分钟 minute,取值 0~59: 第二个参数代表的是小时 hour,取值 0~23: 第三个参数代表的是天 day,取值 1~31: 第四个参数代表的是月 ...
- Win10系统无法使用小米手机的远程管理功能
今天想用电脑往手机传点东西,想到可以用小米手机的远程管理功能. 其实就是手机开了一个ftp服务,在电脑上访问手机ftp.没想到啊,居然出错了: 为啥呢,访问不了?我的电脑上文件和打印机共享都开了的. ...
- sqlserver存储过程sp_send_dbmail邮件(html)实际应用
前段时间因工作需求,特地学习了下sp_send_dbmail的使用,发现网上的示例对我这样的菜鸟太不友好/(ㄒoㄒ)/~~,好不容易完工来和大家分享一下,不谈理论,只管实践! 如下是实际需求: -- ...
- 知识点:Mysql 基本用法之视图
视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的临时 ...
- udev example -- detect usb and write test file
之前学习了下Udev,就随便做了个测试小程序.....设计什么的也没考虑,就实现了一个基本功能,插入U盘,识别,循环检测到有特定文件后,就然后往U盘里面写数据,插拔多次,都能正常工作. 里面的warn ...
- nginx的proxy_pass路径转发规则最后带/问题
一.location匹配路径末尾没有 / location /sta{proxy_pass http://192.168.1.1/sta;} 外面访问:http://外网IP/sta/sta1.htm ...
- day34进程相关
进程1 什么是进程 进程指的是一个正在进行/运行的程序,进程是用来描述程序执行过程的虚拟概念 进程vs程序 程序:一堆代码 进程:程序的执行的过程 进程的概念起源于操作系 ...