题目:判断给定字符串中的括号是否合法。题目中涉及三种符号'(' + ')' , '[' + ']' , '{' + '}'。

思路:利用stack来存储符号。

   注意申请char型stack是: Stack<Character> op = new Stack<Character>();

     stack.peek() 查看栈顶元素,但是不弹出;stack.pop() 查看栈顶元素,并弹出。

解题:

public boolean isValid(String s) {
int len = s.length();
Stack<Character> op = new Stack<Character>();
for(int i = 0 ; i < len ; i++){
char cur = s.charAt(i);
if(!op.isEmpty()){
char top = op.peek();
if(top == '(' && cur == ')' || top == '{' && cur == '}' || top == '[' && cur == ']') op.pop();
else op.push(cur);
}else{
op.push(cur);
}
}
return op.isEmpty();
}

轻松AC,咻~

[leetcode]_Valid Parentheses的更多相关文章

  1. [LeetCode] Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  2. [LeetCode] Valid Parentheses 验证括号

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

  3. N-Queens And N-Queens II [LeetCode] + Generate Parentheses[LeetCode] + 回溯法

    回溯法 百度百科:回溯法(探索与回溯法)是一种选优搜索法,按选优条件向前搜索,以达到目标.但当探索到某一步时,发现原先选择并不优或达不到目标,就退回一步又一次选择,这样的走不通就退回再走的技术为回溯法 ...

  4. LeetCode: Valid Parentheses 解题报告

    Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...

  5. LeetCode Generate Parentheses (DFS)

    题意 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...

  6. LeetCode: Generate Parentheses 解题报告

    Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of w ...

  7. LeetCode——Generate Parentheses

    Description: Given n pairs of parentheses, write a function to generate all combinations of well-for ...

  8. [LeetCode]Generate Parentheses题解

    Generate Parentheses: Given n pairs of parentheses, write a function to generate all combinations of ...

  9. [Leetcode] valid parentheses 有效括号对

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

随机推荐

  1. Nginx 配置指令location 匹配符优先级和安全问题【转】

    Nginx配置指令location匹配符优先级和安全问题 使用nginx 很久了,它的性能高,稳定性表现也很好,得到了很多人的认可.特别是它的配置,有点像写程序一样,每行命令结尾一个";&q ...

  2. 【转】深入理解DIP、IoC、DI以及IoC容器

    原文链接:http://www.cnblogs.com/liuhaorain/p/3747470.html 前言 对于大部分小菜来说,当听到大牛们高谈DIP.IoC.DI以及IoC容器等名词时,有没有 ...

  3. tomcat 内存溢出

    PermGen space的全称是Permanent Generation space,是指内存的永久保存区域,这一部分用于存放Class和Meta的信息,Class在被 Load的时候被放入Perm ...

  4. 查看.netframeword版本

    打开“我的电脑“,在地址栏输入 %systemroot%\Microsoft.NET\Framework

  5. myBatis实例

    一.搭建环境, 建立数据库 CREATE TABLE user( id ) not NULL AUTO_INCREMENT, userName ) DEFAULT NULL, userAge ) DE ...

  6. go get 获得 golang.org 的项目

    go get 用来动态获取远程代码包的,目前支持的有BitBucket.GitHub.Google Code和Launchpad.这个命令在内部实际上分成了两步操作:第一步是下载源码包,第二步是执行g ...

  7. centos设置开机自启动

    编辑 /etc/rc.d/rc.local 将要开启的服务添加到该文件即可

  8. [kuangbin带你飞]专题二十 斜率DP

            ID Origin Title   20 / 60 Problem A HDU 3507 Print Article   13 / 19 Problem B HDU 2829 Lawr ...

  9. Redis链接上不的问题

    问题描述: 同样配置的redis及系统环境,在两台服务器(A.B两台服务)上部署,但是其中一台(A),运行一段时间,就链接不上了,从开始运行redis到redis链接不上,这个时间间隔,不一定有时候是 ...

  10. 常见的MIME类型

    超文本标记语言文本 .htm,.html text/html 普通文本 .txt text/plain GIF图形 .gif image/gif JPEG图形 .ipeg,.jpg image/jpe ...