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, ...
随机推荐
- POJ 1384 Intervals (区间差分约束,根据不等式建图,然后跑spfa)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1384 Intervals Time Limit: 10000/5000 MS (Java/Others ...
- Gradle Goodness: Renaming Files while Copying
With the Gradle copy task we can define renaming rules for the files that are copied. We use the ren ...
- 【星云测试】开发者测试-采用精准测试工具对Spring Boot应用进行测试
简介:本文主要介绍把现今主流的springboot框架项目和精准测试工具进行结合和应用,通过精准测试的数据穿透.数据采集.测试用例与代码的双向追溯.数据分析等一系列精准测试的特有功能,达到对项目质量的 ...
- react路由传参
方法1 <刷新页面参数会消失> <Link className="item" to={{pathname:'/order',params:{index :&quo ...
- [WCF学习笔记] 我的WCF之旅(1):创建一个简单的WCF程序
近日学习WCF,找了很多资料,终于找到了Artech这个不错的系列.希望能从中有所收获. 本文用于记录在学习和实践WCF过程中遇到的各种基础问题以及解决方法,以供日后回顾翻阅.可能这些问题都很基础,可 ...
- 【visual studio code 的python开发环境搭建 】
打开vs code,按按F1或者Ctrl+Shift+P打开命令行,然后输入ext install 输入Python,选第一个,这个用的最多,支持自动补全代码等功能,点击安装按钮,即可安装 下面试着编 ...
- php实现银联支付
银联支付用的还是比较少的,而且开发中也没接触多少,不过因为工作项目用银联支付能降低费率,所以还是接入了银联支付.本文支付为银联网关和WAP支付接口. 官方网站SDK&DEMO:https:// ...
- [试玩] FMXLinux (Firemonkey for Linux) Linux 桌面开发(第三方插件)
FMXLinux 是一个可以用来开发 Linux 桌面软件的第三方插件,它需要配合 Delphi 10.2 Toyko 官网:http://www.fmxlinux.com/ 使用方法:开启 FMX ...
- em,rem区别比较
rem是基于html元素的字体大小来决定,而em则根据使用它的元素的大小决定. 注意:很多人错误以为em是根据父类元素,实际上是使用它的元素继承了父类元素的属性才会产生的错觉. 主要区别 em 和 r ...
- IDEA 通过插件jetty-maven-plugin使用 jetty
jetty:run -Djetty.port=8080 pom.xml配置 <build> <plugins> <plugin> <groupId>or ...