423. Valid Parentheses【LintCode java】
Description
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
Example
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
Challenge
O(n)的时间,n为括号的个数
解题:括号匹配问题,用栈来做较为简单,代码如下:
public class Solution {
/**
* @param s: A string
* @return: whether the string is a valid parentheses
*/
public boolean isValidParentheses(String s) {
// write your code here
Stack<Character>stack = new Stack<Character>();
for(int i = 0; i < s.length(); i++){
if(s.charAt(i) == '(' || s.charAt(i) == '{' || s.charAt(i) == '['){
stack.push(s.charAt(i));
}else{ //pop前要先检查栈是不是空的
if(!stack.isEmpty()){
switch(s.charAt(i)){
case ')':
if(!stack.pop().equals('(')){
return false;
}
break;
case ']':
if(!stack.pop().equals('[')){
return false;
}
break;
case '}':
if(!stack.pop().equals('{')){
return false;
}
break;
}
}else{
return false;
}
}
}
if(stack.isEmpty()){
return true;
}else{
return false;
}
}
}
423. Valid Parentheses【LintCode java】的更多相关文章
- 415. Valid Palindrome【LintCode java】
Description Given a string, determine if it is a palindrome, considering only alphanumeric character ...
- 389. Valid Sudoku【LintCode java】
Description Determine whether a Sudoku is valid. The Sudoku board could be partially filled, where e ...
- 376. Binary Tree Path Sum【LintCode java】
Description Given a binary tree, find all paths that sum of the nodes in the path equals to a given ...
- 372. Delete Node in a Linked List【LintCode java】
Description Implement an algorithm to delete a node in the middle of a singly linked list, given onl ...
- 451. Swap Nodes in Pairs【LintCode java】
Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...
- 445. Cosine Similarity【LintCode java】
Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...
- 433. Number of Islands【LintCode java】
Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...
- 422. Length of Last Word【LintCode java】
Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...
- 420. Count and Say【LintCode java】
Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...
随机推荐
- HDU 1142 A Walk Through the Forest(最短路+记忆化搜索)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- FD.io社区中国行暨未来网络技术沙龙·南京站 会议小结
What is FD.io VPP? FD.io VPP(Fast Data Input/Output Vector Packet Processing)is a new network multi- ...
- nRF5 SDK for Mesh(四) 源码编译
官方文档教程编译源码: http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk%2Fdita%2Fs ...
- html标签种类
标签 描述 <!--...--> 定义注释. <!DOCTYPE> 定义文档类型. <a> 定义锚. <abbr> 定义缩写. <acronym& ...
- docker 容器不能访问宿主端口原因
因为数据包到了eth0的 上的iptables 表,首先匹配PREROUTING 链,这个拒绝了来自docker0的流量,从而跳到input链,input没有放开服务端口,所以容器访问宿主端口失败;但 ...
- PLSQL数据库无选项问题解决
添加如下ORACLE_HOME和OCI library 添加之后,关闭plsql再打开会出现如下界面: 其中database为空. 我当时也是在环境变量里面配置了TNS_ADMIN的环境变量为:如下 ...
- Spring boot 零配置开发微服务
2018年12月29日星期六 体验Spring boot 零配置开发微服务 1.为什么要用Spring boot? 1.1 简单方便.配置少.整合了大多数框架 1.2 适用于微服务搭建,搭建的微服务 ...
- Java调用WeChat's API总结
微信公众号结合着内置浏览器,有着普通浏览器无法完成的服务,前者可以获取浏览页面的微信用户的信息,从而根据信息为用户提供基于微信的更多服务:而后者仅仅能够浏览页面,通过用户的输入信息与用户互动. 本人根 ...
- 笔记:HTML5中input元素新增的type值
在HTML5中,input元素的type值增加了不少,使input的功能强大了很多. 但在各大浏览器中并不是所有的type值都支持. 以下是比较有用.并且浏览器支持的稍好一些的值: type=colo ...
- MongoDB DBA 实践4-----创建复制集
一.复制 复制允许多个数据库服务器共享相同数据的功能,从而确保冗余并促进负载平衡 1.数据冗余及可用性 复制技术提供数据冗余及可用性,在不同的数据库服务器上使用多个数据副本,复制技术防止单个数据库服务 ...