原题
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
- 1
题目大意
给定n对括号,输出他们所有正确的组合
解题思路
采用递归求解试
代码实现
算法实现类
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* Author: 王俊超
* Date: 2015-06-22
* Time: 11:13
* Declaration: All Rights Reserved !!!
*/
public class Solution {
public List<String> generateParenthesis(int n) {
// 保存结果的队列
List<String> result = new ArrayList<>();
// 括号数大于0
if (n > 0) {
// 捛使用数组
char[] parentheses = new char[2 * n];
// 问题求解
solve(n, n, parentheses, result);
}
return result;
}
/**
* @param left 剩余可用的左括号数
* @param right 剩余可用的右括号数
* @param parentheses 到上一次为止括号使用的情况
* @param result 存放结果的集合
*/
public void solve(int left, int right, char[] parentheses, List<String> result) {
// 剩下的括号数不能小于0,并且每次剩下的右括号数都不能小于左括号数
if (left < 0 || right < 0 || right < left) {
// 什么都不用做
}
// 左右括号都被使用完了
else if (left == 0 && right == 0) {
result.add(new String(parentheses));
}
// 可以使用
else {
// 当前使用的位置
int idx = parentheses.length - left - right;
// 使用左括号
parentheses[idx] = '(';
// 递归求解
solve(left - 1, right, parentheses, result);
// 使用右括号
parentheses[idx] = ')';
solve(left, right - 1, parentheses, result);
}
}
}
描述
现在,有一行括号序列,请你检查这行括号是否配对。
输入
第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每组输入数据都是一个字符串S(S的长度小于10000,且S不是空串),测试数据组数少于5组。数据保证S中只含有"[","]","(",")"四种字符
输出
每组输入数据的输出占一行,如果该字符串中所含的括号是配对的,则输出Yes,如果不配对则输出No
样例输入
3 [(]) (]) ([[]()])
样例输出
No No Yes
[java] view plain copy
- import java.util.Scanner;
- import java.util.Stack;
- /**
- * @author Owner
- *
- */
- public class Main {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int n= sc.nextInt();//3条测试数据数据
- Stack<Character> stack = null;
- while(n!=0){
- //从控制台读入一个测试字符串[]() [(])
- String str = sc.next();
- //如果该输入字符串为奇数,说明不匹配
- if(str.length() % 2 == 1){
- System.out.println("No");
- }else{
- //说明字符是偶数
- stack = new Stack<Character>();
- //遍历第一条测试字符串[]() [(])
- for(int i=0;i<str.length();i++){
- if(stack.isEmpty()){
- //如果栈是空的
- stack.push(str.charAt(i));
- }else if(stack.peek() == '[' && str.charAt(i) == ']' || stack.peek() == '(' && str.charAt(i) == ')'){
- //说明此时栈中字符不是空的,并且符合,
- stack.pop();
- }else{
- stack.push(str.charAt(i));
- }
- }
- if(stack.isEmpty()){
- //如果栈是空的,说明括号匹配
- System.out.println("Yes");
- }else{
- //说明栈不为空,括号不匹配
- System.out.println("No");
- }
- }
- n--;
- }
- }
- }
根据题意分析。我们先取n=3.画出所有的情况。
代码
[java] view plain copy
- package parenthesis;
- public class Parenthesis {
- static void printParenthesis(int pos , int n , int open ,int close ,char[] buffer){
- //System.out.println("step"+pos+" open is : "+ open + "close is :" + close);
- //System.out.println(new String(buffer));
- if(close == n){
- //System.out.println("over");
- System.out.println(new String(buffer));
- return;
- }
- if(open >close){
- buffer[pos]='}';
- printParenthesis(pos+1, n, open, close+1, buffer);
- }
- if(open <n){
- buffer[pos] = '{';
- printParenthesis(pos+1, n, open+1, close, buffer);
- }
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- //System.out.println("012142");
- int n = 4;
- char[] cs = new char[8];
- printParenthesis(0, 4, 0, 0, cs);
- //System.out.println("012143");
- }
- }