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】的更多相关文章

  1. 415. Valid Palindrome【LintCode java】

    Description Given a string, determine if it is a palindrome, considering only alphanumeric character ...

  2. 389. Valid Sudoku【LintCode java】

    Description Determine whether a Sudoku is valid. The Sudoku board could be partially filled, where e ...

  3. 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 ...

  4. 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 ...

  5. 451. Swap Nodes in Pairs【LintCode java】

    Description Given a linked list, swap every two adjacent nodes and return its head. Example Given 1- ...

  6. 445. Cosine Similarity【LintCode java】

    Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...

  7. 433. Number of Islands【LintCode java】

    Description Given a boolean 2D matrix, 0 is represented as the sea, 1 is represented as the island. ...

  8. 422. Length of Last Word【LintCode java】

    Description Given a string s consists of upper/lower-case alphabets and empty space characters ' ', ...

  9. 420. Count and Say【LintCode java】

    Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...

随机推荐

  1. EF Core 2.1 Raw SQL Queries (转自MSDN)

    Entity Framework Core allows you to drop down to raw SQL queries when working with a relational data ...

  2. C#中Array类

    Array类是C#中所有数组的基类,它是在System命名空间中定义的,Array类提供了各种用于数组的属性和方法

  3. GoBelieve IM 消息推送的方案

    消息推送设计方案如下: 所有接入im SDK的deviceTOken都会存储到IM服务器.就可以 IM服务器来根据你们服务器指定的useId来下发消息.判断客户端在线,并且APP在前台.就是socke ...

  4. MySQL的笔记

    一.   SELECT tmp2.name,tmp2.browseNum FROM (SELECT tmp.`name`, COUNT(tmp.id) AS browseNum FROM(SELECT ...

  5. 通过代码退出IOS程序

    -(void) tapClick:(UITapGestureRecognizer *)tap{ [UIViewbeginAnimations:@"exitApplication"c ...

  6. throws、throw和try catch

    在学习代理模式的时候,编写动态生成代理类.java文件时,用try{}catch(){}捕获异常发现catch(Exception e)报错,得换成catch(Throwable e),然后又查了查两 ...

  7. SSM(SpringMVC+Spring+Mybatis)框架学习理解

    近期做到的项目中,用到的框架是SSM(SpringMVC+Spring+Mybatis).之前比较常见的是SSH.用到了自然得了解各部分的分工 spring mvc 是spring 处理web层请求的 ...

  8. S2-01

    类: 类是对象的虚例,类是对象的集合,类是对象的模板. 对象是类的实例.(可以 0到多个.) .NET Framwork vs版本 .NET Framwork 1.0 /1.1 vs2003 .NET ...

  9. c#将List转换成DataTable(采用Emit)

    前段时间通过网上查找,使用emit将Datatable,DataReader转换成List<T>了.这是从数据库到展示. 但是最近整理Hikari(我写的数据库连接池),发现c#里面数据库 ...

  10. 服务端接收不到ajax post请求的参数

    问题描述 服务端使用request.getParameter()接收不到post请求的参数,导致业务逻辑抛出空指针异常. 解决途径 tomcat对post请求支持的字节数不受限制的配置发生变化.在to ...