题目:

给定一个只包括 '('')''{''}''['']' 的字符串,判断字符串是否有效。

有效字符串需满足:

  1. 左括号必须用相同类型的右括号闭合。
  2. 左括号必须以正确的顺序闭合。

注意空字符串可被认为是有效字符串。

解:

核心思想: 利用栈的性质,先进后出,遇到左括号则压入栈,遇到右括号则与栈顶元素匹配,若匹配成功则将栈顶元素弹出,反之返回false。

class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>(); for(int i=0; i<s.length(); i++){
char c = s.charAt(i);
if(c == '(' || c == '[' || c == '{')
stack.push(c);
else{
if(stack.isEmpty()){
return false;
}
char top = stack.pop();
if (c == ')' && top != '(') return false;
if (c == '}' && top != '{') return false;
if (c == ']' && top != '[') return false;
}
}
return stack.isEmpty();
}
}

参考与推荐:

1、https://leetcode-cn.com/problems/valid-parentheses/solution/you-xiao-de-gua-hao-by-leetcode/

leetcode之有效的括号(20)的更多相关文章

  1. LeetCode:有效的括号【20】

    LeetCode:有效的括号[20] 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. ...

  2. [LeetCode]678. 有效的括号字符串、20. 有效的括号(栈)

    题目 678. 有效的括号字符串 给定一个只包含三种字符的字符串:( ,) 和 *,写一个函数来检验这个字符串是否为有效字符串.有效字符串具有如下规则: 任何左括号 ( 必须有相应的右括号 ). 任何 ...

  3. [LeetCode] Valid Parentheses 验证括号

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

  4. [LeetCode] Generate Parentheses 生成括号

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

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

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

  6. LeetCode Generate Parentheses 构造括号串(DFS简单题)

    题意: 产生n对合法括号的所有组合,用vector<string>返回. 思路: 递归和迭代都可以产生.复杂度都可以为O(2n*合法的括号组合数),即每次产生出的括号序列都保证是合法的. ...

  7. LeetCode 8 有效的括号

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

  8. leetcode 最长有效括号

    给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 示例 1: 输入: "(()" 输出: 2 解释: 最长有效括号子串为 "()&quo ...

  9. [Leetcode] valid parentheses 有效括号对

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

随机推荐

  1. Meven父工程子模块的SSM框架实现银行转账

            <Meven父工程子模块的SSM框架实现银行转账>                              课程实验报告 实验名称 Meven父工程子模块的SSM框架实现 ...

  2. JDOJ 2225 工资计划

    JDOJ 2225: 工资计划 https://neooj.com/oldoj/problem.php?id=2225 Description 高考结束后,同学们大都找到了一份临时工作,渴望挣得一些零 ...

  3. Codeforces Round #549 (Div. 2) F 数形结合 + 凸包(新坑)

    https://codeforces.com/contest/1143/problem/F 题意 有n条形如\(y=x^2+bx+c\)的抛物线,问有多少条抛物线上方没有其他抛物线的交点 题解 \(y ...

  4. 【HDU6327】Random Sequence(记忆化搜索)

    点此看题面 大致题意: 给你两个序列\(a,v\),其中\(a\)数组由\(0\sim m\)组成.随机用\(1\sim m\)中的一个数替换\(a\)中的\(0\),求\(\sum_{i=1}^{n ...

  5. 怎样删掉vc++ 对话框中的蓝色虚线框

    选择"格式"--"切换辅助线"或者是快捷键alt+o , g

  6. [LeetCode] 829. Consecutive Numbers Sum 连续数字之和

    Given a positive integer N, how many ways can we write it as a sum of consecutive positive integers? ...

  7. [E] Shiro 官方文档阅读笔记 The Reading Notes of Shiro's Offical Docs

    官方文档: https://shiro.apache.org/reference.html https://shiro.apache.org/java-authentication-guide.htm ...

  8. git取消【删除】已经提交的文件(夹)跟踪

    git rm -r --cached <fold> 不删除本地文件 git rm -r --f <fold> 删除本地文件 git rm --cached <file&g ...

  9. JS存取Cookies值

    这里对cookie进行了说明,也介绍了几个方法,但是我要取我存的cookie时取不到,他的方法只是针对存的  名字-值,不涉及键,所以自己写了个方法,来满足我的需求. 封装了简单存取Cookie: / ...

  10. Azure DevOps Server (TFS) 代码库Repo管理培训

    Repo是分布式代码库Git中的一个专用词,用于存储和管理开发团队中特定的源代码. 使用Git,可以协调整个团队的代码更改. 即使您只是一个开发人员,版本控制也可以帮助您在修复错误和开发新功能时保持井 ...