n对括号的有效组合数

参考:https://zh.wikipedia.org/wiki/%E5%8D%A1%E5%A1%94%E5%85%B0%E6%95%B0

import java.util.ArrayList;
import java.util.Stack; public class Num9_6 {
//判断是否是合法的括号字符串
/*
* 遇到左括号入栈,遇到右括号则栈顶元素出栈(在栈不为空的情况下,栈为空则返回false),遇到其他字符则返回false
* 判断栈是否为空,为空则合法,不为空则不合法
*/
public boolean chkParenthesis(String A, int n) {
// write code here
if (n < 2)
return false;
Stack<Character> sc = new Stack<Character>();
for (int i = 0; i < A.length(); i++) {
if (A.charAt(i) == '(')
sc.push('(');
else if (A.charAt(i) == ')') {
if (sc.isEmpty())
return false;
sc.pop();
} else
return false;
}
if (sc.isEmpty())
return true;
return false;
}
//求n对括号的有效组合
/*
* 遵循括号的构成语法,如果左括号还有剩余则使用左括号,如果剩余的右括号比左括号多,则可以使用右括号
* 左括号与右括号都用尽了,则新生成了一个括号组合
*/
ArrayList<String> ans = new ArrayList<String>();
public ArrayList<String> getParenthesis(int n) {
String s = "";
getAll(n, n, s);
return ans;
}
public void getAll(int left, int right, String s) {
if (left == 0 && right == 0) {
ans.add(s);
}
if (left > 0) {
getAll(left - 1, right, s + "(");
}
if (right > 0 && right > left) {
getAll(left, right - 1, s + ")");
}
}
}

判断括号字符串是否为合法+求n对括号的所有组合的更多相关文章

  1. C# 判断一字符串是否为合法数字(正则表达式)

    判断一个字符串是否为合法整数(不限制长度) public static bool IsInteger(string s) { string pattern = @"^\d*$"; ...

  2. python实现判断一个字符串是否是合法IP地址

    #!usr/bin/env python #encoding:utf-8 ''''' __Author__:沂水寒城 功能:判断一个字符串是否是合法IP地址 ''' import re def jud ...

  3. 判断一个字符串是否是合法IP地址

    # -*- coding: utf-8 -*- """ @File:test06_判断ip地址是否合法.py @E-mail:364942727@qq.com @Time ...

  4. 判断一个字符串是否为合法IP

    输入任意一个字符串,判断是否为合法IP bool IsIPAddress(const char * str){ //先判断形式是否合法, //检查是否只包含点和数字 ; str[i] != '\0'; ...

  5. python点滴:判断字符串是否为合法json格式

    在一些情况下,我们需要判断字符串是否为合法json格式. 思路很简单:尝试对字符串使用json.loads(),如果不是合法json格式,则会抛出ValueError异常. 示例如下: import ...

  6. 求n对括号的合法组合

    一道经典的面试题,求n对括号有多少种合法的组合. 抽象为2n个位置,每个位置可以有2种取值,总共有2^2n个组合,附加约束条件是要符合括号的语法,用来剪枝. 括号语法的合法性条件: 初始化左括号和右括 ...

  7. [LeetCode] 678. Valid Parenthesis String 验证括号字符串

    Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...

  8. javascript 写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数

    javascript 写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数 function test(){ var bt = document.getElementById(" ...

  9. [LeetCode] Valid Parenthesis String 验证括号字符串

    Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...

随机推荐

  1. LeetCode 392. Is Subsequence

    Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...

  2. mysql的数据类型与列属性

  3. apk反编译查看源码

    1.将apk解压

  4. hdu1030

    #include<stdio.h>#include<math.h>void find(int n,int &l,int &r,int &level){ ...

  5. Chapter 2 Open Book——33

    My chin raised a fraction. 我的下巴抬起来了一点. 我略微抬起下颚. "No, she did not send me here. I sent myself.&q ...

  6. JS 一些常用技巧

    记录 JS 常用的技巧 1. 生成随机数 2. 解决浮点数问题 3. 无路可走时,看看是不是 事件 冒泡了...

  7. IntentService和Service的区别

    整个看下来是一个Service+Thread+handle的结合体, Service:比Activity的被kill的级别低 Thread:不阻塞UI线程 Handle:队列式的消息循环 那这个玩意的 ...

  8. HashMap按键排序和按值排序

    对map集合进行排序   今天做统计时需要对X轴的地区按照地区代码(areaCode)进行排序,由于在构建XMLData使用的map来进行数据统计的,所以在统计过程中就需要对map进行排序. 一.简单 ...

  9. Nginx redirect

    if ($host != 'www.xxxxx.com' ) { rewrite ^/(.*)$ http://www.xxxx.com/$1 permanent; }

  10. ubuntu 配置ftp服务器 vsftpd

    1. 更新库,否则会可能有库过时不匹配报错. sudo apt-get update 2. 安装vsftpd sudo apt-get install vsftpd 3. 判断vsftpd是否安装成功 ...