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,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
随机推荐
- Android软键盘遮挡的四种解决方案
问题概述 在编辑框输入内容时会弹出软键盘,而手机屏幕区域有限往往会遮住输入界面,我们先看一下问题效果图: 输入用户名和密码时,系统会弹出键盘,造成系统键盘会挡住文本框的问题,如图所示: 输入密码时输入 ...
- C语言qsort函数用法
qsort函数简介 排序方法有很多种:选择排序,冒泡排序,归并排序,快速排序等. 看名字都知道快速排序是目前公认的一种比较好的排序算法.因为他速度很快,所以系统也在库里实现这个算法,便于我们的使用. ...
- 111个知名Java项目集锦,包括url和描述
转:http://www.cnblogs.com/wangs/p/3282183.html 项目名称 项目描述 ASM Java bytecode manipulation framework A ...
- 用Filter解决乱码和jsp缓存问题
1) 乱码Filter: 新建一个:CharSetFilter package com.my.filter; import java.io.*; import javax.servlet.*; imp ...
- SQL SERVER 组内排序
取出每组的第一个 select *from (select * ,RANK ( ) OVER( PARTITION by org order by reportcode asc) PartionNum ...
- webstorm配置nodejs,bower,git,github
一,配置nodejs第一大步,首先安装nodejs,安装nodejs的时候,我们需要把所有的组建勾选上,然后选择add to path,这一步会自动帮我们配置环境变量,安装完成后,打开cmd,输入no ...
- php接二进制文件
PHP默认只识别application/x-www.form-urlencoded标准的数据类型. 因此,对型如text/xml 或者 soap 或者 application/octet-stream ...
- golang rbac框架
在 https://github.com/mikespook/gorbac/tree/v1.0 github上新的版本是开发板,得用这里的老版 demo package main import ( & ...
- 源码安装extundelete以及对遇到问题的解决
软件下载:http://sourceforge.net/projects/extundelete/ 1.在安装extundelete包./configure时遇到configure: error: C ...
- 在 Perl 中使用 Getopt::Long 模块来接收用户命令行参数
我们在linux常常用到一个程序需要加入参数,现在了解一下 perl 中的有关控制参数的模块 Getopt::Long ,比直接使用 @ARGV 的数组强大多了.我想大家知道在 Linux 中有的参数 ...