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 {

  1. if (s == null)
  2. return false;
  3. Stack<Character> stack = new Stack<Character>();
  4. char[] ch = s.toCharArray();
  5. for (int i = 0; i < ch.length; i++) {
  6. if ((ch[i] == '(') || (ch[i] == '[') || (ch[i] == '{'))
  7. stack.push(ch[i]);
  8. else {
  9. if (stack.isEmpty())
  10. return false;
  11. if (ch[i] - stack.pop() > 2)
  12. return false;
  13. }
  14. }
  15. return stack.isEmpty();

  

  1. public boolean isValid(String s) {
  2. if (s == null) //改进1:可以不用String.charat(i)

  3. return false; //改进2: 先判断长度
    //改进三: 不需要这么多if else ,符号的判断可以用ascii码
  4. Stack<Character> stack = new Stack<Character>();
  5. char[] ch = s.toCharArray();
  6. for (int i = 0; i < ch.length; i++) {
  7. if ((ch[i] == '(') || (ch[i] == '[') || (ch[i] == '{'))
  8. stack.push(ch[i]);
  9. else {
  10. if (ch[i] == ')') {
  11. if (stack.isEmpty())
  12. return false;
  13. else if (stack.pop() != '(')
  14. return false;
  15. }
  16. if (ch[i] == ']') {
  17. if (stack.isEmpty())
  18. return false;
  19. if (stack.pop() != '[')
  20. return false;
  21. }
  22. if (ch[i] == '}') {
  23. if (stack.isEmpty())
  24. return false;
  25. if (stack.pop() != '{')
  26. return false;
  27. }
  28. }
  29. }
  30. if (!stack.isEmpty())
  31. return false;
  32. else
  33. return true;
  34. }
  35. }

  

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. Android软键盘遮挡的四种解决方案

    问题概述 在编辑框输入内容时会弹出软键盘,而手机屏幕区域有限往往会遮住输入界面,我们先看一下问题效果图: 输入用户名和密码时,系统会弹出键盘,造成系统键盘会挡住文本框的问题,如图所示: 输入密码时输入 ...

  2. C语言qsort函数用法

    qsort函数简介 排序方法有很多种:选择排序,冒泡排序,归并排序,快速排序等. 看名字都知道快速排序是目前公认的一种比较好的排序算法.因为他速度很快,所以系统也在库里实现这个算法,便于我们的使用. ...

  3. 111个知名Java项目集锦,包括url和描述

    转:http://www.cnblogs.com/wangs/p/3282183.html 项目名称   项目描述 ASM Java bytecode manipulation framework A ...

  4. 用Filter解决乱码和jsp缓存问题

    1) 乱码Filter: 新建一个:CharSetFilter package com.my.filter; import java.io.*; import javax.servlet.*; imp ...

  5. SQL SERVER 组内排序

    取出每组的第一个 select *from (select * ,RANK ( ) OVER( PARTITION by org order by reportcode asc) PartionNum ...

  6. webstorm配置nodejs,bower,git,github

    一,配置nodejs第一大步,首先安装nodejs,安装nodejs的时候,我们需要把所有的组建勾选上,然后选择add to path,这一步会自动帮我们配置环境变量,安装完成后,打开cmd,输入no ...

  7. php接二进制文件

    PHP默认只识别application/x-www.form-urlencoded标准的数据类型. 因此,对型如text/xml 或者 soap 或者 application/octet-stream ...

  8. golang rbac框架

    在 https://github.com/mikespook/gorbac/tree/v1.0 github上新的版本是开发板,得用这里的老版 demo package main import ( & ...

  9. 源码安装extundelete以及对遇到问题的解决

    软件下载:http://sourceforge.net/projects/extundelete/ 1.在安装extundelete包./configure时遇到configure: error: C ...

  10. 在 Perl 中使用 Getopt::Long 模块来接收用户命令行参数

    我们在linux常常用到一个程序需要加入参数,现在了解一下 perl 中的有关控制参数的模块 Getopt::Long ,比直接使用 @ARGV 的数组强大多了.我想大家知道在 Linux 中有的参数 ...