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: ...
随机推荐
- sql server 查看数据库配置等信息(字符集,编码格式,版本号...)
select SERVERPROPERTY(N'edition') as Edition --数据版本,如企业版.开发版等,SERVERPROPERTY(N'collation') as Collat ...
- XSS靶机
第一关 localhost:8083/xss/level1.php?name=test<script>alert(1)</script> 第二关 源码 文本框输入js代码,查看 ...
- etcd raft 处理流程图系列2-transport
本章给出了raftexample中使用的传输层代码,补全了上一节中传输层与raft节点(raft server和channel server)的交互细节.下图中流程的核心在于传输层中的streamRt ...
- 自学linux——3.编辑器vim的使用
编辑器之神--vim 一. vim的三种模式 1.命令模式(打开文件后默认模式) 不能直接对文件编辑,可以输入快捷键进行一些操作 2.编辑模式 对文件的内容进行编辑 3.末行模式(尾行模式) ...
- 一个故事看懂HTTPS
我是一个浏览器,每到夜深人静的时候,主人就打开我开始学习. 为了不让别人看到浏览记录,主人选择了"无痕模式". 但网络中总是有很多坏人,他们通过抓包截获我和服务器的通信,主人干了什 ...
- 腾讯云TDSQL PostgreSQL版 -最佳实践 |优化 SQL 语句
查看是否为分布键查询 postgres=# explain select * from tbase_1 where f1=1; QUERY PLAN ------------------------- ...
- 基于Java和Bytemd用120行代码实现一个桌面版Markdown编辑器
前提 某一天点开掘金的写作界面的时候,发现了内置Markdown编辑器有一个Github的图标,点进去就是一个开源的Markdown编辑器项目bytemd(https://github.com/byt ...
- iptables 及容器网络分析
本文独立博客阅读地址:https://ryan4yin.space/posts/iptables-and-container-networks/ 本文仅针对 ipv4 网络 iptables 提供了包 ...
- C语言自学第一天
直接上代码 1 #include<stdio.h> 2 #include<math.h> 3 /*定义符号常量(预处理)注:可为各种类型*/ 4 #define STUDY & ...
- DVWA靶场之XSS(Stored)通关
Low: <?php if( isset( $_POST[ 'btnSign' ] ) ) { // Get input $message = trim( $_POST[ 'mtxMessage ...