20. Valid Parentheses (JAVA)
Given a string containing just the characters '('
, ')'
, '{'
, '}'
, '['
and ']'
, determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Example 1:
Input: "()"
Output: true
Example 2:
Input: "()[]{}"
Output: true
Example 3:
Input: "(]"
Output: false
Example 4:
Input: "([)]"
Output: false
Example 5:
Input: "{[]}"
Output: true
class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<>(); //Primitive types such as char cannot be used as type parameters in Java. You need to use the wrapper type for(int i = 0; i < s.length(); i++){
if(s.charAt(i) == '(' || s.charAt(i) == '[' || s.charAt(i) == '{'){
stack.push(s.charAt(i));
}
else if(s.charAt(i) == ')') {
if(stack.empty() || stack.peek() != '(') return false;
stack.pop();
}
else if(s.charAt(i) == ']'){
if(stack.empty() || stack.peek() != '[') return false;
stack.pop();
}
else if(s.charAt(i) == '}'){
if(stack.empty() || stack.peek() != '{') return false;
stack.pop();
}
}
if(!stack.empty()) return false;
return true;
}
}
20. Valid Parentheses (JAVA)的更多相关文章
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- [Leetcode][Python]20: Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...
- 20. Valid Parentheses【leetcode】
20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- 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练题——20. Valid Parentheses
1.题目 20. Valid Parentheses——Easy Given a string containing just the characters '(', ')', '{', '}', ...
- 刷题20. Valid Parentheses
一.题目说明 这个题目是20. Valid Parentheses,简单来说就是括号匹配.在学数据结构的时候,用栈可以解决.题目难度是Medium. 二.我的解答 栈涉及的内容不多,push.pop. ...
- C# 写 LeetCode easy #20 Valid Parentheses
20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- 2016-08-15T16:00:00.000Z 格式转换成yyyy-MM-dd HH:mm:ss格式
public static String UTCStringtODefaultString(String UTCString) { try { if (CommonUtils.notNullAndEm ...
- Windows10中使用Anaconda安装keras-gpu版本(遇到的坑)
1.使用conda install tensorflow-gpu 2.使用pip install keras 这里使用pip安装而不是使用conda,原因是使用conda安装会默认安装cpu版本的te ...
- 借助ssh隧道和中间主机,使本地主机可以直连远程主机
本地主机: localhost 中间主机: kickstart服务器 10.164.229.162 远程主机: fuel 服务器 192.168.0.11 背景:正常情况下,本地不能直 ...
- iOS 12 tabbar 从二级页面返回时,出现跳动解决办法
APP push一个界面,返回的时候,tabBar上的图标和文字出现一个从上往下的神奇动画 经过测试发现,如果使用系统OS12.1 UINavigationController + UITabBarC ...
- List Leaves
Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. I ...
- java 通过jmx获取active mq队列消息
一.修改active mq配置文件 修改\conf\activemq.xml,带下划线部分 <!-- Licensed to the Apache Software Foundation (AS ...
- java 垃圾回收总结(1)(转)
转自: http://www.cnblogs.com/aigongsi/archive/2012/04/06/2434771.html 另外可参考: http://ifeve.com/gc-orien ...
- .net core2 单元测试
1.下载 https://marketplace.visualstudio.com/items?itemName=RandomEngy.UnitTestBoilerplateGenerator 2 ...
- OO第一单元(求导)单元总结
OO第一单元(求导)单元总结 这是我们oo课程的第一个单元,也是意在让我们接触了解掌握oo思想的一个单元,这个单元的作业以求导为主题,从一开始的加减多项式求导再到最后的嵌套多项式求导,难度逐渐提高,编 ...
- 升级linux python
# python -V # 查看python 版本 # cd /home/centos/Downloads # 进入存放目录 # wget https://www.python.org/ftp/pyt ...