20. Valid Parentheses(stack)
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 {
if (s == null)
return false;
Stack<Character> stack = new Stack<Character>();
char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; i++) {
if ((ch[i] == '(') || (ch[i] == '[') || (ch[i] == '{'))
stack.push(ch[i]);
else {
if (stack.isEmpty())
return false;
if (ch[i] - stack.pop() > 2)
return false;
}
}
return stack.isEmpty();
public boolean isValid(String s) {
if (s == null) //改进1:可以不用String.charat(i)
return false; //改进2: 先判断长度
//改进三: 不需要这么多if else ,符号的判断可以用ascii码
Stack<Character> stack = new Stack<Character>();
char[] ch = s.toCharArray();
for (int i = 0; i < ch.length; i++) {
if ((ch[i] == '(') || (ch[i] == '[') || (ch[i] == '{'))
stack.push(ch[i]);
else {
if (ch[i] == ')') {
if (stack.isEmpty())
return false;
else if (stack.pop() != '(')
return false;
}
if (ch[i] == ']') {
if (stack.isEmpty())
return false;
if (stack.pop() != '[')
return false;
}
if (ch[i] == '}') {
if (stack.isEmpty())
return false;
if (stack.pop() != '{')
return false;
}
}
}
if (!stack.isEmpty())
return false;
else
return true;
}
}
20. Valid Parentheses(stack)的更多相关文章
- [Leetcode] 20. Valid Parentheses(Stack)
括号匹配问题,使用栈的特点,匹配则出栈,否则入栈,最后栈为空则全部匹配.代码如下: class Solution { public: bool isValid(string s) { stack< ...
- LeetCode:20. Valid Parentheses(Easy)
1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')', ...
- 32. Longest Valid Parentheses (Stack; DP)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- LeetCode 之 Longest Valid Parentheses(栈)
[问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest v ...
- 20. Valid Parentheses (python版)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode 20 Valid Parentheses (括号匹配问题)
题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description Problem: 括号匹配问题. 使用栈,先进后出! ...
- Leetcode 之Longest Valid Parentheses(39)
有一定的难度.用堆栈记录下所有左符的位置,用变量记录下孤立右符的位置. int longestValidParentheses(const string& s) { stack<int& ...
- 【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】
[032-Longest Valid Parentheses(最长有效括号)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string contai ...
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
随机推荐
- json解析:[2]fastjson 使用
利用阿里的fastjson包对对象进行 json的转化与解析,本篇为第二篇,第一篇讲述的是利用gson进行json数据解析,地址:jingyan.baidu.com/article/e8cdb32b6 ...
- eclipse color themes 让eclipse编码好看点
http://eclipsecolorthemes.org/ 就是这个,很好用的! 安装后需要重启Eclipse.重启后打开Window->Preferences->General-> ...
- exp命令ORACLCE10G导出ORACLE11G的数据1455错误
异常:1455 解决: 命令:exp youruser/password@192.xxx.x.xx:1521/orcl owner=youruser file=c:\export.dmp trigge ...
- Android 程式开发:(十三)特殊碎片 —— 13.2 DialogFragment
Android 程式开发:(十三)特殊碎片 —— 13.2 DialogFragment 原文地址 我们也可以创建另外一种碎片——DialogFragment.顾名思义,DialogFragment就 ...
- XAMPP 在windows下无法启动Apache解决方案
XAMPP 在windows下无法启动Apache解决方案 一.现象 XAMPP 点击Start Apache时出现如下错误 20:41:12 [Apache] Error: Apache shut ...
- [原]对Linux环境下任务调度一点认识
我一直以来有一个误解,那就是在终端运行某个程序时,按下Ctrl + D时我误以为就是杀死了这个进程,今天才知道原来不是.比如我利用libevent在Linux环境下写了一个网络监听程序,当启动程序之后 ...
- ettercap ARP dns 欺骗
1.arp 这个简单,太熟了.略过1 2.dns 根据arp欺骗的步骤. 多了个etter.dns文件 找到它:locate etter.dns 进入后添加dns正向解析 启动,选 ...
- POJ #1042 Gone Fishing - WA by a DP solution. TODO
I used DP instead of Greedy. But got WA on PoJ, though it passed all web-searched cases. Maybe I hav ...
- C#:基于WMI查询USB设备
来源:http://blog.csdn.net/jhqin/article/details/6734673 /* ------------------------------------------- ...
- 4. Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...