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

思路:利用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. Maven pom.xml 配置详解

    http://niuzhenxin.iteye.com/blog/2042102 http://blog.csdn.net/u012562943/article/details/51690744 po ...

  2. 关于ie6下拖动滚动条时,div抖动的问题解决

    你如果遇到了这个问题,算是你有福了. 首先说非ie6下的div不随滚动条变化而移动位置的. 1,首先在body中写足够多的文字,一直到浏览器出现滚动条.例如你可以拼命的放P,足够多的P标签 2建立一个 ...

  3. 关于 Python Iterator 协议的一点思考

    转:http://www.jianshu.com/p/dcf83643deeb Python 中有好几种容器或者序列类型:list tuple dict set str,对于这些类型中的内容,往往需要 ...

  4. C#局域网聊天工具_UDP广播

    接上一讲,程序启动就要发送广播消息,如何发送广播消息,这一讲将给大家好好讲讲网络广播的知识,以及C#如何实现广播. 第一部分.什么是广播地址,以及广播地址怎么计算 1.1 广播地址是什么? 主机号全为 ...

  5. nyoj 105 九的余数

    点击打开链接 九的余数 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述 现在给你一个自然数n,它的位数小于等于一百万,现在你要做的就是求出这个数整除九之后的余数. 输入 ...

  6. (easy)LeetCode 231.Power of Two

    Given an integer, write a function to determine if it is a power of two. Credits:Special thanks to @ ...

  7. java cmd 命令

    java的Runtime.getRuntime().exec(commandStr)可以调用执行cmd指令. cmd /c dir 是执行完dir命令后关闭命令窗口. cmd /k dir 是执行完d ...

  8. JAVA设计模式之依赖倒转原则

    3.1 依赖倒置原则的定义 依赖倒置原则(Dependence Inversion Principle,简称DIP)这个名字看着有点别扭,“依赖”还“倒置”,这到底是什么意思?依赖倒置原则的原始定义是 ...

  9. 编写javascript、Jquery的String.format();

    在javascript.Jquery里面好像是没有String.format();这个函数的,所以我们在拼接字符串的时候就特别的辛苦,生怕又打错,而且又乱,所以就自己去写一个函数来代替. String ...

  10. Android 进阶 Fragment 介绍和使用 (一)

    Fragment概述 Fragment是activity的界面中的一部分或一种行为.你可以把多个Fragment们组合到一个activity中来创建一个多面界面并且你可以在多个activity中重用一 ...