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. 1.高并发教程-基础篇-之nginx负载均衡的搭建

    温馨提示:请不要盲目的进行横向扩展,优先考虑对单台服务器的性能优化,只有单台服务器的性能达到最优化之后,集群才会被最大的发挥作用. 一.架构图: 服务器准备:3台,ubuntu16.04系统maste ...

  2. 基于.net core 微服务的另类实现

    基于.net core 的微服务,网上很多介绍都是千篇一律基于类似webapi,通过http请求形式进行访问,但这并不符合大家使用习惯.如何像形如[ GetService<IOrderServi ...

  3. Oracle 存储结构一

    了解块中表行数据的存储 Oracle数据存储模型 逻辑结构在左,物理结构在右 有一个关系使用虚线绘制,表示段与数据文件的多对多关系.之所以使用虚线表示关系,是因为这种多对多关系不应存在. 表空间实体消 ...

  4. iOS:位置相关(18-03-09更)

    1.定位设置 2.定位页面逻辑 1.定位设置 2.定位页面逻辑 1).第一次进入该VC,在 viewDidLoad 调用刷新页面 refreshLocationView .这时用户还没决定,会刷出“正 ...

  5. JavaScript变量类型检测总结

    JavaScript中的变量类型: 基本类型值:Undefined,Null,Boolean,Number和String. 按值访问(可直接操作保存在变量中的变量值): 复制规则:当复制基本类型值时: ...

  6. 【Linux】YUM源搭建

    YUM是什么? YUM是什么 基于rpm但更胜于rpm的软件管理工具: YUM有服务端和客户端: 如果服务端和客户端在同一台机器,这是本地YUM: 如果服务端和客户端不在同一台机器,这是网络YUM. ...

  7. 第三月 day03.笔记

    函数在调用的时候回形成一个私有作用域,内部变量不会被外面访问,这种保护机制叫做闭包,这就意味着函数调用完了,这个函数形成的栈内存就会被销毁,但有时候我们不希望被销毁. * 函数归属谁和他的调用没有关系 ...

  8. 14JavaScript条件语句

    条件语句用于基于不同的条件来执行不同的动作. 1.条件语句 通常在写代码时,您总是需要为不同的决定来执行不同的动作.您可以在代码中使用条件语句来完成该任务. 在 JavaScript 中,我们可使用以 ...

  9. 使用babel

    1).Babel支持NPM包形式的安装,打开命令行窗口,切换到项目根目录,命令如下 npm install babel-cli 2).安装成功后,在package.json文件里添加如下代码 &quo ...

  10. 大数据时代的结构化存储--HBase

    迄今,相信大家肯定听说过 HBase,但是对于 HBase 的了解可能仅仅是它是 Hadoop 生态圈重要的一员,是一个大数据相关的数据库技术. 今天我带你们一起领略一下 HBase 体系架构,看看它 ...