有效的括号

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

有效字符串需满足:

左括号必须用相同类型的右括号闭合。

左括号必须以正确的顺序闭合。

示例说明请见LeetCode官网。

来源:力扣(LeetCode)

链接:https://leetcode-cn.com/problems/valid-parentheses/

著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

解法一:利用栈

初始化一个左括号栈leftParentheses,遍历字符串s的每个字符,当遇到左括号时,将当前字符入栈,当遇到右括号时,判断leftParentheses栈顶的字符是否是当前字符对应的左括号,如果不是,返回无效;否则出栈。遍历完成后,判断leftParentheses是否为空,如果不为空,说明左括号没有对应的右括号,返回无效;否则,有效。

import java.util.Stack;

public class Solution {
public static boolean isValid(String s) {
if (s == null || s.length() == 0) {
return true;
}
Stack<Character> leftParentheses = new Stack<>();
for (char parentheses : s.toCharArray()) {
if (parentheses == '(' || parentheses == '{' || parentheses == '[') {
leftParentheses.push(parentheses);
} else if (parentheses == ')') {
if (leftParentheses.size() > 0 && leftParentheses.peek() == '(') {
leftParentheses.pop();
} else {
return false;
}
} else if (parentheses == ']') {
if (leftParentheses.size() > 0 && leftParentheses.peek() == '[') {
leftParentheses.pop();
} else {
return false;
}
} else if (parentheses == '}') {
if (leftParentheses.size() > 0 && leftParentheses.peek() == '{') {
leftParentheses.pop();
} else {
return false;
}
}
}
if (leftParentheses.size() != 0) {
return false;
}
return true;
} public static void main(String[] args) {
System.out.println(isValid("()[]{}"));
}
}

【每日寄语】每个睡醒后的早晨都是一件礼物,把每个开心后的微笑当成一个习惯,美好的一天从现在开始。

LeetCode-020-有效的括号的更多相关文章

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

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

  2. [LeetCode] Generate Parentheses 生成括号

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

  3. [LeetCode] Valid Parentheses 验证括号

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

  4. [Leetcode][020] Valid Parentheses (Java)

    题目在这里: https://leetcode.com/problems/valid-parentheses/ [标签]Stack; String [个人分析]这个题应该算是Stack的经典应用.先进 ...

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

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

  6. [LeetCode] 20. 有效的括号 (栈)

    思路: 首先用字典将三对括号存储,遍历字符串中每个字符,遇到左括号就入栈:遇到右括号就开始判断:是否与栈弹出的顶字符相同. 如果到最后栈被清空,说明全部匹配上了,为真. class Solution( ...

  7. LeetCode 20. 有效的括号(Valid Parentheses)

    20. 有效的括号 20. Valid Parentheses 题目描述 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须 ...

  8. Java实现 LeetCode 20 有效的括号

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

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

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

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

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

随机推荐

  1. 中文分词,自然语言处理器NLP。 新版本已上线,增加二级行业分类。

    一  cacl2新版本上线,在一级行业的基础上深度挖掘,新增了对应的二级分类. 可以查看一级行业[农林牧渔],下面对应的所有二级行业的词库.这里拿[林业]来观察效果. 具体分词的效果. 二    Gi ...

  2. Java 中对象锁和类锁的区别? 关键字 Synchronized的用法?

    一  对象锁和类锁的关系 /* * 对象锁和[类锁] 全局锁的关系? 对象锁是用于对象实例方法,或者一个对象实例上的 this 类锁是用于类的静态方法或者一个类的class对象上的. Ag.class ...

  3. 多源最短路-Floyd

    题目描述 时间限制:5.0s 内存限制:256.0MB 问题描述 给定\(n\)个结点两两之间的单向边的长度,求两两之间的最短路径. 输入格式 输入第一行包含一个整数\(n\),表示点数. 接下来\( ...

  4. NumPy 教程目录

    NumPy 教程目录 1 Lesson1--NumPy NumPy 安装 2 Lesson2--NumPy Ndarray 对象 3 Lesson3--NumPy 数据类型 4 Lesson4--Nu ...

  5. JS RegExp对象(正则表达式)

    笔记整理自:廖雪峰老师的JS教程 正则表达式语法:https://www.runoob.com/regexp/regexp-tutorial.html 目录 创建方式 方式一 方式二 简单使用 判断正 ...

  6. Linux Makefile 生成 *.d 依赖文件及 gcc -M -MF -MP 等相关选项说明

    1. 为什么要使用后缀名为 .d 的依赖文件? 在 Makefile 中, 我们的依赖关系可能需要包含一系列的头文件.比如main.c 源文件内容如下: #include "stdio.h& ...

  7. Android编译implement、api 和compile区别【转】

    感谢大佬:https://blog.csdn.net/fengyeNom1/article/details/81903186 前言 2017 年google 后,Android studio 版本更新 ...

  8. docker简介及安装(1)

    Docker简介 软件开发中最为麻烦的事情可能就是配置环境了.由于用户使用的操作系统具有多样性,即便使用跨平台的开发语言(如Java和Python)都不能保证代码能够在各种平台下都可以正常的运转,而且 ...

  9. 随机数类 Random

    import java.util.Random; /* 随机数类 Random 需求: 编写一个函数随机产生四位的验证码. */ public class Demo5 { public static ...

  10. python基础——生成器与迭代器

    生成器 def func(): print("111") yield 1 print("222") yield 3 print("333") ...