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的更多相关文章

  1. [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 ...

  2. 动态规划——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 ...

  3. 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 ...

  4. [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 ...

  5. leetcode903 Valid Permutations for DI Sequence

    思路: dp[i][j]表示到第i + 1个位置为止,并且以剩下的所有数字中第j + 1小的数字为结尾所有的合法序列数. 实现: class Solution { public: int numPer ...

  6. [Swift]LeetCode921.使括号有效的最少添加 | Minimum Add to Make Parentheses Valid

    Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...

  7. [leetcode-921-Minimum Add to Make Parentheses Valid]

    Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...

  8. 921. Minimum Add to Make Parentheses Valid

    Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...

  9. [LeetCode] 921. Minimum Add to Make Parentheses Valid 使括号有效的最少添加

    Given a string S of '(' and ')' parentheses, we add the minimum number of parentheses ( '(' or ')', ...

随机推荐

  1. Day 2:线程与进程系列问题(二)

    补充: 线程的创建方式二: 1.自定义一个实现Runnable接口的类 2.实现Runnable接口中的run方法把自定义线程的任务写在run方法中 3.创建实现Runnable接口的对象 4.创建T ...

  2. 阿里P7Java最全面试296题:阿里天猫、蚂蚁金服含答案文档解析

    [阿里天猫.蚂蚁.钉钉面试专题题目加答案] 不会做别着急:文末有答案以及视频讲解,架构师资料 1. junit用法,before,beforeClass,after, afterClass的执行顺序 ...

  3. python----linux下简单的排序

    1.选择排序:把一个数与余下所有的数排序,最小的排到最前面 [root@besttest liyn_test]# cat test.py #! /usr/bin/python a=[,,,] ,len ...

  4. 读取word模板,填充数据后导出

    一.需求说明 定期生成word报告,报告中含有文本.表格.图表等元素,依次获取进行替换,保留原有样式,生成新的word文档 二.引入依赖 <dependency> <groupId& ...

  5. html_位置偏移属性position

    定位属性 位置属性position:static.relative.absolute.fixed 偏移属性:top.bottom.left.right 浮动定位属性:float/clear 1.浮动定 ...

  6. C++保存数据到CSV文件

    主要是今天工作的时候需要把一些数据保存到本地,因为是一些预测值和标签的对比,还有预测值的概率,所以想到用CSV文件来保存,大概查了一下,还是比较简单的,所以记录一下. 首先要说明的是CSV文件有点类似 ...

  7. SQL基础教程(第2版)第7章 集合运算:7-1 表的加减法

    第7章 集合运算:7-1 表的加减法 ● 集合运算就是对满足同一规则的记录进行的加减等四则运算.● 使用UNION(并集). INTERSECT(交集). EXCEPT(差集)等集合运算符来进行集合运 ...

  8. Thread--生产者消费者

    2个生产者,2个消费者,库存容量2 package p_c_allWait.copy; import java.util.LinkedList; import java.util.List; publ ...

  9. house_cat 's blog

    本人蒟蒻,ACM退役选手 可能会刷刷CF,写一下笔记,学一点JAVA 欢迎指正:QQ:1468580561 不要忘记努力,不要辜负自己

  10. linux文件软链接与硬链接

    1.命令格式: ln [参数][源文件或目录][目标文件或目录] 软链接只会在你选定的位置上生成一个文件的镜像,不会占用磁盘空间. 2.命令功能: Linux文件系统中,有所谓的链接(link),我们 ...