思路:采用递归的思想,当左括号数大于右括号数时可以加左或者右括号,否则只能加左括号,当左括号数达到n时,剩下全部。不过,每一个方法的调用都会产生一个栈帧,每执行一个方法就会出现压栈操作,所以采用递归的时候产生的栈帧比较多,递归就会影响到内存,非常消耗内存。当左括号数大于右括号数时可以加左或者右括号,否则只能加左括号,当左括号数达到n时,剩下全部。

public static void main(String[] args){
  ArrayList<String> res = new ArrayList<String>();
  Scanner input = new Scanner(System.in);
  System.out.println("请输入n:");
  int n = input.nextInt();
  generate(res, "", 0, 0, n);
  System.out.println(res);
}
public static void generate(ArrayList<String> res, String tmp, int lhs, int rhs, int n){
  if(lhs == n){
    for(int i = 0; i < n - rhs; i++){
      tmp += ")";
    }
    res.add(tmp);
    return ;
  }
  generate(res, tmp + "(", lhs + 1, rhs, n);
  if(lhs > rhs)
    generate(res, tmp + ")", lhs, rhs + 1, n);
}

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: "((()))", "(()())", "(())()", "()(())", "()()()"的更多相关文章

  1. [Javascript] Write a Generator Function to Generate the Alphabet / Numbers

    When you need to generate a data set, a generator function is often the correct solution. A generato ...

  2. [LeetCode] Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  3. LeetCode 22. Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  4. 【leetcode】Generate Parentheses

    题目简述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

  5. No.022:Generate Parentheses

    问题: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...

  6. Generate Parentheses

    Generate Parentheses Given n pairs of parentheses, write a function to generate all combinations of ...

  7. 【leetcode】 Generate Parentheses (middle)☆

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  8. [LintCode] Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  9. 【JAVA、C++】LeetCode 022 Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

随机推荐

  1. js函数中的BOM和DOM

    BOM 浏览器对象模型  screen对象 console.log(screen.width);// 屏幕宽度 console.log(screen.height);// 屏幕高度 console.l ...

  2. css3实现可以计算的自适应布局——calc()

    开始我们需要先了解什么是calc() ,calc()是一个CSS函数,你可以使用calc()给元素的margin.pading.width等属性设置 而且你还可以在一个calc()内部嵌套另一个cal ...

  3. 使用Iterator的方式也可以顺利删除和遍历

    使用Iterator的方式也可以顺利删除和遍历 eg: public void iteratorRemove() { List<Student> students = this.getSt ...

  4. docker - 启动container时出现 [warning] : ipv4 forwarding is disabled. networking will not work

    起因 今天在一台新的centos宿主机上安装docker,由于关闭了iptables,在此之后启动container的时候会出现警告: WARNING: IPv4 forwarding is disa ...

  5. [leetcode-623-Add One Row to Tree]

    Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with value ...

  6. javascript数组集锦

    设计数组的函数方法 toString, toLocaleString, valueOf, concat, splice, slice indexOf,lastIndexOf, push, pop, s ...

  7. 高级Java程序员的技术进阶之路

      据不完全统计,截至目前(2017.07)为止,中国Java程序员的数量已经超过了100万.而且,随着IT培训业的持续发展和大量的应届毕业生进入社会,Java程序员面临的竞争压力越来越大.那么,作为 ...

  8. wget访问SOAP接口

    SOAP协议主要是XML交互,所以其访问过程类似于这样: wget --header='Content-Type: text/xml;charset=utf-8' --post-data='<s ...

  9. geotrellis使用(二十九)迁移geotrellis至1.1.1版

    目录 前言 升级过程 总结 一.前言        由于忙着安装OpenStack等等各种事情,有半年的时间没有再亲密的接触geotrellis,甚至有半年的时间没能畅快的写代码.近来OpenStac ...

  10. Frameset框架集的应用

    Frameset框架集常用于写网站后台页面,大多数"T字型"布局后台页面,就是应用Frameset框架集来做的.Franeset框架集的优点是,他可以在同浏览器窗口显示不同页面内容 ...