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)的更多相关文章

  1. [Leetcode] 20. Valid Parentheses(Stack)

    括号匹配问题,使用栈的特点,匹配则出栈,否则入栈,最后栈为空则全部匹配.代码如下: class Solution { public: bool isValid(string s) { stack< ...

  2. LeetCode:20. Valid Parentheses(Easy)

    1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')',  ...

  3. 32. Longest Valid Parentheses (Stack; DP)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  4. LeetCode 之 Longest Valid Parentheses(栈)

    [问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest v ...

  5. 20. Valid Parentheses (python版)

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

  6. LeetCode 20 Valid Parentheses (括号匹配问题)

    题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description   Problem: 括号匹配问题. 使用栈,先进后出!   ...

  7. Leetcode 之Longest Valid Parentheses(39)

    有一定的难度.用堆栈记录下所有左符的位置,用变量记录下孤立右符的位置. int longestValidParentheses(const string& s) { stack<int& ...

  8. 【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】

    [032-Longest Valid Parentheses(最长有效括号)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a string contai ...

  9. LeetCode解题笔记 - 20. Valid Parentheses

    这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...

随机推荐

  1. C#实现数字字符串左补齐0的3种方法

    int n = 3; string s = n.ToString().PadLeft(4, '0'); //0003 s = string.Format("{0:d4}", n); ...

  2. CentOS 系统配置完jdk,tomcat mysql,nginx 项目发布步骤

    1.启动项目之前,一定要先启动nginx服务 重启nginx服务的命令:./nginx -s reload 2.然后启动三个tomcat的服务 3.如果只能进入tomcat,不能进入项目:把tomca ...

  3. LintCode "Delete Digits"

    Greedy: remove earliest down-edge: like "54", "97". class Solution { public: /** ...

  4. SPOJ #442 Searching the Graph

    Just CS rookie practice on DFS\BFS. But details should be taken care of: 1. Ruby implementation got ...

  5. 【原创】Quartz代码详解

    阅读目录 简单介绍 章节1:Quartz简单实例 章节2:Job.JobDetail.JobBuilder 章节3:Trigger.TriggerBuilder 章节4:Scheduler 章节5:J ...

  6. bzoj3007: 拯救小云公主

    Description     英雄又即将踏上拯救公主的道路……     这次的拯救目标是——爱和正义的小云公主.     英雄来到boss的洞穴门口,他一下子就懵了,因为面前不只是一只boss,而是 ...

  7. /proc 文件系统

    linux提供了一种特殊的文件系统procfs,通常以/proc目录的形式呈现.该目录中包含了许多特殊文件用来对驱动程序和内核信息进行更高层的访问.只要应用程序有正确的访问全息,就可以通过读写这些文件 ...

  8. 一个SQLSERVER触发器的示例

    CREATE TRIGGER WoStateChange on T_PD_WorkOrder AFTER UPDATE AS BEGIN declare @WorkOrderID varchar(20 ...

  9. PHP “Warning: session_start()...”、"correct (..\..\php5\Temp) in Unknown on line 0" 的解决方法

    运行php的时候出现了一下警告: Warning: Unknown: open(D:/Program Files/php5/temp1\sess_l5b1a48m6kmb1g0t5cs33690v0, ...

  10. [UEditor]百度编辑器配置总结

    前端配置文件ueditor.config.js 前端有两个重要的配置属性: UEDITOR_HOME_URL: 配置百度编辑器的资源目录路径,你可以手动指定此路径,默认是有URL变量指定,而URL变量 ...