1. 括号(0809)

设计一种算法,打印n对括号的所有合法的(例如,开闭一一对应)组合。

说明:解集不能包含重复的子集。

例如,给出 n = 3,生成结果为:

[

"((()))",

"(()())",

"(())()",

"()(())",

"()()()"

]

class Solution {
List<String> list = new ArrayList<>();
char[] arr = new char[]{'(',')'};
public List<String> generateParenthesis(int n) {
StringBuilder sb = new StringBuilder();
backtrack(sb,n,0,0);
return list;
}
//countl表示左括号的数量,countr表示右括号的数量
public void backtrack(StringBuilder sb,int n,int countl,int countr){
if(countl < countr || countl > n){
return;
}
if(sb.length() == 2*n){
list.add(sb.toString());
}
for(char c : arr){
sb.append(c);
if(c == '('){
countl++;
backtrack(sb,n,countl,countr);
sb.deleteCharAt(sb.length()-1);
countl--;
}else{
countr++;
backtrack(sb,n,countl,countr);
sb.deleteCharAt(sb.length()-1);
countr--;
} } }
}

2. 有效的括号(20)

class Solution {
public boolean isValid(String s) {
int len = s.length();
if(len % 2 == 1) return false;
char[] arr = s.toCharArray();
Stack<Character> sin = new Stack<>();
Stack<Character> sou = new Stack<>();
for(char c : arr){
sin.push(c);
}
while(!sin.isEmpty()){
char tmp = sin.pop();
if(tmp == ']' || tmp == '}' ||tmp == ')')
sou.push(tmp);
else{
if(sou.isEmpty()) return false;
boolean b = isValidhelper(tmp,sou.pop());
if(!b) return false;
}
}
return true;
}
public boolean isValidhelper(char c1,char c2){
if(c1 == '(' && c2 == ')') return true;
if(c1 == '[' && c2 == ']') return true;
if(c1 == '{' && c2 == '}') return true;
return false;
}
}

3. 有效的括号(20)

leetcode 括号的更多相关文章

  1. leetcode - 括号字符串是否有效

    括号字符串是否有效 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. ...

  2. [LeetCode] Remove Invalid Parentheses 移除非法括号

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  3. [LeetCode] Different Ways to Add Parentheses 添加括号的不同方式

    Given a string of numbers and operators, return all possible results from computing all the differen ...

  4. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  5. [LeetCode] Generate Parentheses 生成括号

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

  6. [LeetCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  7. leetcode 栈 括号匹配

    https://oj.leetcode.com/problems/valid-parentheses/ 遇到左括号入栈,遇到右括号出栈找匹配,为空或不匹配为空, public class Soluti ...

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

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

  9. [LeetCode] Score of Parentheses 括号的分数

    Given a balanced parentheses string S, compute the score of the string based on the following rule: ...

随机推荐

  1. Java中Arrays数组的定义与使用

    初始化 Java中数组是固定长度,数组变量是个对象. NullPointerException 空指针异常. ArrayIndexOutOfBoundsException 索引值越界. 数组三种初始化 ...

  2. 一文带你认识LPWA通信技术

    摘要:为了满足越来越多的远距离物联网设备的连接需求,LPWA应用而生. 本文分享自华为云社区<常见物联网通信技术之LPWA通信技术>,作者:爱吃面包的猫. 如果你比较关注物联网圈的话,想必 ...

  3. SQL Server 行转列 列转行操作

    1.多行转成一行(并以','分开) 表数据如下图: 查询结果如下图: SQL查询脚本: SELECT addPer, house_code = (STUFF((SELECT ',' + house_c ...

  4. openssl查看证书命令

    openssl x509部分命令打印出证书的内容:openssl x509 -in cert.pem -noout -text打印出证书的系列号openssl x509 -in cert.pem -n ...

  5. Java基础——变量与常量及命名规范

    变量 ()  public class Demon04{     //类变量 static     static double salary=2500;     //属性: 变量 ​     //实例 ...

  6. C++ //继承同名成员处理方式

    1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 class Base 6 { 7 pu ...

  7. Kong网关安装之Docker版(2)

    1.安装kong管理工具:konga或者kong-dashboard,这里选择konga 拉取konga镜像: sudo docker pull pantsel/konga:0.14.4 初始化kon ...

  8. Emlog V6.0.0代码审计笔记

    前言 emlog是一套基于PHP和MySQL的博客及CMS建站系统. emlog v6.0.0存在后台SQL注入漏洞. 分析 官网下载emlog最新版v6.0.0,本地搭建. 前台功能不多,参数基本都 ...

  9. 从零开始实现简单 RPC 框架 3:配置总线 URL

    URL 的定义 URL 对于大部分程序猿来说都是很熟悉的,其全称是 Uniform Resource Locator (统一资源定位器).它是互联网的统一资源定位标志,也就是指网络地址. 一个标准的 ...

  10. .Net Core with 微服务 - 分布式事务 - TCC

    上一次我们讲解了分布式事务的 2PC.3PC .那么这次我们来理一下 TCC 事务.本次还是讲解 TCC 的原理跟 .NET 其实没有关系. TCC Try 准备阶段,尝试执行业务 Confirm 完 ...