1. 括号(0809)

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

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

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

[

"((()))",

"(()())",

"(())()",

"()(())",

"()()()"

]

  1. class Solution {
  2. List<String> list = new ArrayList<>();
  3. char[] arr = new char[]{'(',')'};
  4. public List<String> generateParenthesis(int n) {
  5. StringBuilder sb = new StringBuilder();
  6. backtrack(sb,n,0,0);
  7. return list;
  8. }
  9. //countl表示左括号的数量,countr表示右括号的数量
  10. public void backtrack(StringBuilder sb,int n,int countl,int countr){
  11. if(countl < countr || countl > n){
  12. return;
  13. }
  14. if(sb.length() == 2*n){
  15. list.add(sb.toString());
  16. }
  17. for(char c : arr){
  18. sb.append(c);
  19. if(c == '('){
  20. countl++;
  21. backtrack(sb,n,countl,countr);
  22. sb.deleteCharAt(sb.length()-1);
  23. countl--;
  24. }else{
  25. countr++;
  26. backtrack(sb,n,countl,countr);
  27. sb.deleteCharAt(sb.length()-1);
  28. countr--;
  29. }
  30. }
  31. }
  32. }

2. 有效的括号(20)

  1. class Solution {
  2. public boolean isValid(String s) {
  3. int len = s.length();
  4. if(len % 2 == 1) return false;
  5. char[] arr = s.toCharArray();
  6. Stack<Character> sin = new Stack<>();
  7. Stack<Character> sou = new Stack<>();
  8. for(char c : arr){
  9. sin.push(c);
  10. }
  11. while(!sin.isEmpty()){
  12. char tmp = sin.pop();
  13. if(tmp == ']' || tmp == '}' ||tmp == ')')
  14. sou.push(tmp);
  15. else{
  16. if(sou.isEmpty()) return false;
  17. boolean b = isValidhelper(tmp,sou.pop());
  18. if(!b) return false;
  19. }
  20. }
  21. return true;
  22. }
  23. public boolean isValidhelper(char c1,char c2){
  24. if(c1 == '(' && c2 == ')') return true;
  25. if(c1 == '[' && c2 == ']') return true;
  26. if(c1 == '{' && c2 == '}') return true;
  27. return false;
  28. }
  29. }

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. sql server 查看数据库配置等信息(字符集,编码格式,版本号...)

    select SERVERPROPERTY(N'edition') as Edition --数据版本,如企业版.开发版等,SERVERPROPERTY(N'collation') as Collat ...

  2. XSS靶机

    第一关 localhost:8083/xss/level1.php?name=test<script>alert(1)</script> 第二关 源码 文本框输入js代码,查看 ...

  3. etcd raft 处理流程图系列2-transport

    本章给出了raftexample中使用的传输层代码,补全了上一节中传输层与raft节点(raft server和channel server)的交互细节.下图中流程的核心在于传输层中的streamRt ...

  4. 自学linux——3.编辑器vim的使用

    编辑器之神--vim 一.      vim的三种模式 1.命令模式(打开文件后默认模式) 不能直接对文件编辑,可以输入快捷键进行一些操作 2.编辑模式 对文件的内容进行编辑 3.末行模式(尾行模式) ...

  5. 一个故事看懂HTTPS

    我是一个浏览器,每到夜深人静的时候,主人就打开我开始学习. 为了不让别人看到浏览记录,主人选择了"无痕模式". 但网络中总是有很多坏人,他们通过抓包截获我和服务器的通信,主人干了什 ...

  6. 腾讯云TDSQL PostgreSQL版 -最佳实践 |优化 SQL 语句

    查看是否为分布键查询 postgres=# explain select * from tbase_1 where f1=1; QUERY PLAN ------------------------- ...

  7. 基于Java和Bytemd用120行代码实现一个桌面版Markdown编辑器

    前提 某一天点开掘金的写作界面的时候,发现了内置Markdown编辑器有一个Github的图标,点进去就是一个开源的Markdown编辑器项目bytemd(https://github.com/byt ...

  8. iptables 及容器网络分析

    本文独立博客阅读地址:https://ryan4yin.space/posts/iptables-and-container-networks/ 本文仅针对 ipv4 网络 iptables 提供了包 ...

  9. C语言自学第一天

    直接上代码 1 #include<stdio.h> 2 #include<math.h> 3 /*定义符号常量(预处理)注:可为各种类型*/ 4 #define STUDY & ...

  10. DVWA靶场之XSS(Stored)通关

    Low: <?php if( isset( $_POST[ 'btnSign' ] ) ) { // Get input $message = trim( $_POST[ 'mtxMessage ...