[Algo] 66. All Valid Permutations Of Parentheses I
Given N pairs of parentheses “()”, return a list with all the valid permutations.
Assumptions
- N > 0
Examples
- N = 1, all valid permutations are ["()"]
- N = 3, all valid permutations are ["((()))", "(()())", "(())()", "()(())", "()()()"]
public class Solution {
public List<String> validParentheses(int n) {
// Write your solution here
List<String> res = new ArrayList<>();
StringBuilder sb = new StringBuilder();
helper(res, n, n, sb);
return res;
}
private void helper(List<String> res, int left, int right, StringBuilder sb) {
if (left == 0 && right == 0) {
res.add(sb.toString());
return;
}
if (left > 0) {
sb.append("(");
helper(res, left - 1, right, sb);
sb.deleteCharAt(sb.length() - 1);
}
if (right > left) {
sb.append(")");
helper(res, left, right - 1, sb);
sb.deleteCharAt(sb.length() - 1);
}
}
}
[Algo] 66. All Valid Permutations Of Parentheses I的更多相关文章
- [Swift]LeetCode903. DI 序列的有效排列 | Valid Permutations for DI Sequence
We are given S, a length n string of characters from the set {'D', 'I'}. (These letters stand for &q ...
- 动态规划——Valid Permutations for DI Sequence
We are given S, a length n string of characters from the set {'D', 'I'}. (These letters stand for &q ...
- 903. Valid Permutations for DI Sequence
We are given S, a length n string of characters from the set {'D', 'I'}. (These letters stand for &q ...
- [LeetCode] 903. Valid Permutations for DI Sequence DI序列的有效排列
We are given S, a length n string of characters from the set {'D', 'I'}. (These letters stand for &q ...
- leetcode903 Valid Permutations for DI Sequence
思路: dp[i][j]表示到第i + 1个位置为止,并且以剩下的所有数字中第j + 1小的数字为结尾所有的合法序列数. 实现: class Solution { public: int numPer ...
- [Swift]LeetCode921.使括号有效的最少添加 | Minimum Add to Make Parentheses Valid
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...
- [leetcode-921-Minimum Add to Make Parentheses Valid]
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...
- 921. Minimum Add to Make Parentheses Valid
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...
- [LeetCode] 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加
Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...
随机推荐
- 解决fixed布局里内容不滚动问题
//注意给父级加上 (1)横向滚动 left:0; right:0; 然后想要横向滚动的话 overflow-x:scroll; overflow-y:hidden; (2)竖直方向滚动top:0;b ...
- Tensorflow Mask-RCNN(三)——实时 检测视频
参考:https://www.youtube.com/watch?v=lLM8oAsi32g import cv2 import numpy as np def ran ...
- unix中嘚vim编辑器
在linux家族中,vim编辑器是系统自带的文本编辑器,其功能强大自不必说了. 偶有小白,刚接触linux,要修改某个文本文件,不可能像WINDOWS那样操作,更有甚者,进入VI编辑器后,无法退出以致 ...
- git登录账号密码错误remote: Incorrect username or password (access token)
git提交时弹框让输入用户和密码,不小心输入错误了 再次提交 一直就提示 remote: Incorrect username or password 错误了,也不弹框要重新输入 解决方法 win1 ...
- 系统学习python第一天学习笔记
1.计算机认识 1.常见的操作系统 win xp win7 win10 window server(服务器) linux centos,图形化界面差 ubuntu , 个人开发(图形化比较好) red ...
- RK3399开发板Android镜像烧写之Windows系统映像烧写
4.1.1 l RKTool 驱动安装(基于迅为iTOP-3399开发板)DriverAssitant_v4.5.zip 文件,打开 驱动安装成功,如下图: 注意事项:1.目前支持的操作系统包括:X ...
- import datetime
import datetimenow = datetime.datetime.now()print('当前时间:',now) 当前时间: 2019-11-21 11:11:58.093122
- vim python支持
yum remove vim -y yum install ncurses-devel python-devel -y git clone https://github.com/vim/vim.git ...
- 定时任务莫名停止,Spring 定时任务存在 Bug?
专注于Java领域优质,技术欢迎关注 作者: 鸭血粉丝 来自:Java极客技术 Hello~各位读者新年好,我是鸭血粉丝(大家可以称呼我为「阿粉」).这里阿粉给大家拜个年,祝大家蒸蒸日上烫烫烫,年年有 ...
- sudo: /etc/sudoers is mode 0777, should be 0440 单用户 sudo不用输入密码的方法
sudo权限问题考察一题 su - sudo su - sudo su - test [root@localhost ~]# su - zzx #root用户进行切换不需要输入密码 [ ...