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方法基础

    java方法基础 方法的定义与调用 方法是命名的语句的有序集,是解决一类问题的步骤的有序组合. 方法包含于类或者是对象中,方法在程序中被创建,在其他地方被引用. 组成:方法是由方法头和方法体两部分组成 ...

  2. Linux的链接(入门)

    Linux的链接分为两种:硬链接和软链接 硬链接:如果B是A的硬链接,那么B和A指向同一个文件,但是删除A并不会影响B->允许一个文件有多个路径 软链接:类似Windows下的快捷方式,删除原文 ...

  3. 并发编程——Java线程的6种状态及切换

    前言 本次主要分享一下Java线程的六种状态及其转换. 如果对于线程的创建方式不太了解,推荐观看并发编程--认识java里的线程 线程的状态及其转换 操作系统线程的五种状态 新建(NEW) 就绪(RU ...

  4. 来看看是什么原因导致生产服上的系统CPU高的?

    我们可能会遇到生产服务器CPU很高的问题,有时候能确定是哪个进程,但是不知道这个进程都在干什么,所以也无从下手,无法解决问题.只能不断的重启,重启等. 最近也看了[一线码农]的一些教程,觉得都很不错, ...

  5. Jetpack Compose学习(1)——从登录页开始入门

    原文地址:Jetpack Compose学习(1)--从登录页开始入门 | Stars-One的杂货小窝 Jetpack Compose UI在前几天出了1.0正式版,之前一直还在观望,终于是出了正式 ...

  6. 关于数字化工厂&智能工厂建设 IT 经验总结

    最近疫情闹得胆战心惊,前不久客户给我开了一个玩笑,当天我们同桌会议了一天,晚上客户回家之后就被隔离了,当他给我发这个消息的时候背都凉了一截,害怕之余在机场呆了一个晚上,捅乐鼻孔插了嗓子之后确认无事,后 ...

  7. remote: Support for password authentication was removed

    周末提交代码,把代码push到github上,控制台报了下面的错误: remote: Support for password authentication was removed on August ...

  8. DVWA-sql注入(盲注)

    DVWA简介 DVWA(Damn Vulnerable Web Application)是一个用来进行安全脆弱性鉴定的PHP/MySQL Web应用,旨在为安全专业人员测试自己的专业技能和工具提供合法 ...

  9. 题解 graph

    传送门 一道做了巨久,不过确实很好的题 发现不定边权极难处理,所以就不会 感觉和这题有点像,但还是不会 但发现题面里有个地方很套路 要求有哪些点/边最终可以满足最短/最小,比如这样或这样的题,考虑凸包 ...

  10. Longhorn,企业级云原生容器分布式存储 - 定制默认设置

    内容来源于官方 Longhorn 1.1.2 英文技术手册. 系列 Longhorn 是什么? Longhorn 云原生容器分布式存储 - 设计架构和概念 Longhorn 云原生容器分布式存储 - ...