[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 ')', ...
随机推荐
- python函数-函数进阶
python函数-函数进阶 一.命名空间和作用域 1.命名空间 内置命名空间 —— python解释器 就是python解释器一启动就可以使用的名字存储在内置命名空间中 内置的名字在启动解释器的时候被 ...
- 201771010123汪慧和《面向对象程序设计Java》第十八周实验总结
一.总复习纲要 1. Java语言特点与开发环境配置(第1章.第2章) 2. Java基本程序结构(第3章) 3. Java面向对象程序结构(第4章.第5章.第6章) 4. 类.类间关系.类图 5. ...
- zabbix监控一个机器上的多个java进程的jvm
一.监控安装部署 1.1 JVM端口配置 (/bqhexin/tomcat/bin/catalina.sh)在安装的tomcat路径,找到catalina.sh文件. vim编辑并添加: catali ...
- CF #610Div2 B2.K for the Price of One (Hard Version) (dp解法 && 贪心解法)
原题链接:http://codeforces.com/contest/1282/problem/B2题目大意:刚开始有 p 块钱,商店有 n 件物品,你每次可以只买一件付那一件的钱,也可以买 k 件只 ...
- Python插件安装
Python插件安装 1. 找到Python的安装目录. 打开CMD控制台输入 python 打开环境变量,找到Python安装路径. 进入 安装目录 下的 Scripts 目录 . 查看已安装的插件 ...
- 实验吧web-易-Forms
打开网页,查看源码, 第二行,showsource的value是0,我们在查看器中将showsource的value值改为1,然后随便输入一个数,可以看到页面出现 意思就是我们输入的PIN的值应该是代 ...
- 201609-2 火车购票 Java
思路待补充 import java.util.Scanner; class Main{ public static void main(String[] args) { //100个座位 int[] ...
- JAVA调用FFMpeg进行转码等操作
直接上代码: public abstract class FFmpegUtils { FFmpegUtils ffmpegUtils; ; String timeLength = "&quo ...
- Ubuntu Teamviewer安装使用
关于Ubuntu环境下teamviewer的安装(亲测可用-) 以下内容均转自:https://blog.csdn.net/weixin_41887832/article/details/798329 ...
- ADB 用法大全 【转】
https://github.com/mzlogin/awesome-adb awesome-adb ADB,即 Android Debug Bridge,它是 Android 开发/测试人员不可替代 ...