20. Valid Parentheses【leetcode】
20. Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
判断{},(),<>是否成对出现
public class Solution {
public boolean isValid(String s) {
Stack<Character> stack=new Stack<>();
for(char c:s.toCharArray()){
if(c=='('||c=='{'||c=='['){
stack.push(c);
}
if(c==')'){
if(stack.isEmpty()||stack.pop()!='('){
return false;
}
}
if(c=='}'){
if(stack.isEmpty()||stack.pop()!='{') {
return false;
}
}
if(c==']'){
if(stack.isEmpty()||stack.pop()!='['){
return false;
}
}
}
return stack.isEmpty();
}
}
思路:
- 首先需要将所有右侧开口(<{进栈,如果遇到了左侧开口的符号则进行出栈
- 如果最后的栈为空,则证明所有的符号都刚好配对成功
- 特别注意:下面这行代码子啊进行判断的时候一定要先判断是否为空再出栈,否则会引发结果不正确
if(stack.isEmpty()||stack.pop()!='('){
20. Valid Parentheses【leetcode】的更多相关文章
- 【LeetCode】字符串 string(共112题)
[3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...
- 【LeetCode】排序 sort(共20题)
链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...
- 【LeetCode】593. Valid Square 解题报告(Python)
[LeetCode]593. Valid Square 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
- [Leetcode][Python]20: Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
- 【LeetCode】678. Valid Parenthesis String 解题报告(Python)
[LeetCode]678. Valid Parenthesis String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人 ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
随机推荐
- 【LeetCode】258. Add Digits
题目: Given a non-negative integer num, repeatedly add all its digits until the result has only one di ...
- 【css技能提升】css高级技巧
本文是一个帮你提升 CSS 技巧的收藏集. 使用CSS复位 CSS复位可以在不同的浏览器上保持一致的样式风格.您可以使用CSS reset 库Normalize等,也可以使用一个更简化的复位方法: * ...
- Linux系统网卡设置
由于做了虚拟机的克隆,发现克隆机和被克隆机的MAC地址相同了,下面我将要介绍一下linux中网卡的配置步骤,我使用的linux是CentOS release 6.9 (Final) 1.root用户编 ...
- Agile&DevOps究竟谁是魔法棒
天下没有神奇的配方 很抱歉,文章的开头我就要说出这个残酷的事实 - 世界上没有任何工具可以魔法般的让你实现敏捷,精益,DevOps.如果只是依赖了工具的自动化,实现了自动化Dev或者Ops,那么别忘了 ...
- 关于MultipleActiveResultSets属性导致的There is already an open DataReader associated with this Command which must be closed first的解决方法
执行SqlDataReader.Read之后,如果还想用另一个SqlCommand执行Insert或者Update操作的话,会得到一个错误提示:There is already an open Dat ...
- OCP 11G 实验环境安装文档 ( RedHat5.5 + Oracle11g )
RedHat5.5 linux下Oracle11g软件安装 一.配置虚拟机 为了创建和配置虚拟机,你需要添加硬件设备如磁盘和cpu,在你开始安装之前,创建一个windows目录作为存放虚拟机的目录 目 ...
- python基础(6):列表和字典类型
前面我们所接触的数据类型都是存单个值,今天我们来看看可以存多个值的列表和字典. 预习: 1.有列表data=['alex',49,[1900,3,18]],分别取出列表中的名字,年龄,出生的年,月,日 ...
- Docker与KVM之间的区别
一.Docker简介 Docker 项目的目标是实现轻量级的操作系统虚拟化解决方案. Docker 的基础是 Linux 容器(LXC)等技术. 在 LXC 的基础上 Docker 进行了进一步的封装 ...
- hdu_1907:John(Nim变形)
题目链接 仍是取石子,不过取到最后一个的败 参考链接:http://www.voidcn.com/blog/liwen_7/article/p-3341825.html 简单一句话就是T2 S0必败 ...
- eclipse上传新项目到GitHub
1.登录gibhub,创建一个仓库 2.提交本地仓库 右键本地项目 选择git,然后点击next 如图: 再右键项目,commit项目,这是提交到本地git 3.接下来提交到github 输入gith ...