leetcode 括号
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 括号的更多相关文章
- leetcode - 括号字符串是否有效
括号字符串是否有效 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. ...
- [LeetCode] Remove Invalid Parentheses 移除非法括号
Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...
- [LeetCode] Different Ways to Add Parentheses 添加括号的不同方式
Given a string of numbers and operators, return all possible results from computing all the differen ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- leetcode 栈 括号匹配
https://oj.leetcode.com/problems/valid-parentheses/ 遇到左括号入栈,遇到右括号出栈找匹配,为空或不匹配为空, public class Soluti ...
- [LeetCode] Valid Parenthesis String 验证括号字符串
Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...
- [LeetCode] Score of Parentheses 括号的分数
Given a balanced parentheses string S, compute the score of the string based on the following rule: ...
随机推荐
- HTTP协议GET方法传参最大长度理解误区
结论 HTTP 协议未规定GET和POST的长度 GET的最大长度是因为浏览器和WEB服务器显示了URI的长度 不同浏览器和WEB服务器,限制的最大长度不同 若要支持IE,则最大长度为2083 byt ...
- NOIP&CSP PJ 难度刷题记录
前言 本来不想写前言的(>人<:) 这只是 mjl 给我们布置的作业,并不是我自己在刷题! 不保证所有代码的正确性,它们仅仅是通过了所有数据点而已. 1.模拟板块 整体难度:红~黄(模拟不 ...
- 小程序中多个echarts折线图在同一个页面的使用
最近做小程序的业务中遇到一个页面要同时显示几个echarts图,刚开始遇到各种冲突,死数据可以,动态数据就报错的问题,折磨了一天,仔细看了官网和查在各种资料之后,终于解决了. 直接上代码: commi ...
- Salesforce Integration 概览(四) Batch Data Synchronization(批量数据的同步)
本篇参考:https://resources.docs.salesforce.com/sfdc/pdf/integration_patterns_and_practices.pdf 前两篇博客讲了一下 ...
- Java数组05——Arrays类
Arrays类讲解 package array; import java.util.Arrays; public class ArrayDemon07 { public static ...
- HDFS中NameNode工作机制
引言 NameNode: 存储元数据 管理整个HDFS集群 DataNode: 存储数据的block SecondaryNameNode: 辅助HDFS完成一些事情 NameNode和Secondar ...
- 使用VNC远程安装CentOS 7操作系统
使用VNC远程安装CentOS 7操作系统 by 无若 数据中心一般都不在本地,如果希望重新安装系统,难道还要跑到数据中心...所以必须要有一种方式来远程解决这个问题. 目前CentOS 7主要使用的 ...
- 深入理解jvm-2Edition-Java内存区域
1.运行时数据区域 Java虚拟机会将内存区域划分为几个区域,每个区域储存不同类型的数据或承担不同的功能. PC,堆-Java堆,栈-虚拟机栈.本地方法栈,方法区.直接内存. 当类被实例化或stati ...
- Git-07-分支管理
创建与合并分支 为什么要创建分支? 假设你准备开发一个新功能,但是需要两周才能完成, 第一周你写了50%的代码,如果立刻提交,由于代码还没写完,不完整的代码库会导致别人不能干活了. 如果等代码全部写完 ...
- 基于RT1052 Aworks 内存扩容记录(一)
本文主要是通过迁移的思维,记录本人初次使用周立功的Aworks框架进行BSP开发 1. 首先阅读原理图 内存容量由32M扩容至64M. 2. 再则比较两颗芯片的参数 通过比较32M和64M SDRAM ...