思路:采用递归的思想,当左括号数大于右括号数时可以加左或者右括号,否则只能加左括号,当左括号数达到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. Linux网络编程“惊群”问题总结

    1.前言 我从事Linux系统下网络开发将近4年了,经常还是遇到一些问题,只是知其然而不知其所以然,有时候和其他人交流,搞得非常尴尬.如今计算机都是多核了,网络编程框架也逐步丰富多了,我所知道的有多进 ...

  2. 小程序解析html标签wxPrase插件

    微信小程序的标签和原来我们习惯用的标签是不一样的,例如视图容器标签小程序是view,然而html就很多比如常用的div就和小程序的view类似. 通常我们在开发小程序(从列表页跳转到详情页)通过富文本 ...

  3. stringsteam使用之整型转字符串

    最近需要用到整型转字符串的操作,学习了stringstream一些皮毛. 首先需要包含头文件. #include<sstream> 然后用流操作的方式将值传递给stringstream对象 ...

  4. java底层学习---1

    JRE: Java Runtime EnvironmentJDK:Java Development Kit JRE顾名思义是java运行时环境,包含了java虚拟机,java基础类库.是使用java语 ...

  5. laravel中间件使用

    1.在app/Http/Kernel.php文件中配置中间件文件,例如: protected $routeMiddleware = [ 'auth' => \Illuminate\Auth\Mi ...

  6. AutoMapper 6.x 扩展

    简介 很多时候我们使用AutoMapper的时候,都需要进行一个配置才可以使用Mapper.Map<Source,Target>(entity);.如果不进行配置则会报错. 如果实体过多, ...

  7. scrollMenu,一款可滚动的菜单插件 兼容pc和移动端

    这个菜单 有两种样式  , 也可以通过animate.css加不同的动画效果!滚动的方式也有两种   一种为通用的overflow,另外一种是better-scroll的滚动效果 在线链接地址  ht ...

  8. 一键将Web应用发布到云-Azure Web App

    我们现在越来越多的传统应用,逐步向云端迁移,原先私有云的部署模式,逐步向云端PaaS IaaS转变.例如: 我们在云端Azure中申请VM虚拟机,将我们的Web应用部署到VM的IIS中,同时做云服务的 ...

  9. Chrome浏览器扩展开发系列之三:Google Chrome浏览器扩展的架构

    1) 不可视的background页面 Google Chrome扩展往往包含一个不可见的background页面,Google Chrome扩展的主要业务逻辑都位于此.有两种类型的backgroun ...

  10. 【转载】 ISO14229系列之一:简介

    转载链接:http://www.cnblogs.com/autogeek/p/4458591.html 前言 由于工作中经常用到ISO-14229,因此决定对该协议做个总体介绍和总结,既是对自己学习的 ...