LeetCode(49)-Valid Parentheses
题目:
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.
思路:
- 题意:一个字符串,只是包含括号,判断括号是不是合法的使用
- 这个问题的思路是使用栈,这一类对应的问题都是使用栈,遇到前括号压栈,遇到后括含出栈,是不是对应,看最后是不是为0
-
代码:
public class Solution {
public boolean isValid(String s) {
Stack<Character> stack = new Stack<Character>();
for(int i = 0;i < s.length();i++){
char a = s.charAt(i);
if(a == '('||a == '{'||a == '['){
stack.push(a);
}else if(a == ')' && !stack.isEmpty() && stack.peek() == '('){
stack.pop();
}else if(a == '}' && !stack.isEmpty() && stack.peek() == '{'){
stack.pop();
}else if(a == ']' && !stack.isEmpty() && stack.peek() == '['){
stack.pop();
}else{
return false;
}
}
return stack.isEmpty();
}
}
LeetCode(49)-Valid Parentheses的更多相关文章
- LeetCode(20)Valid Parentheses
题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the i ...
- LeetCode(36)Valid Sudoku
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- LeetCode(49): 字母异位词分组
Medium! 题目描述: 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. 示例: 输入: ["eat", "tea", ...
- LeetCode(242)Valid Anagram
题目 Given two strings s and t, write a function to determine if t is an anagram of s. For example, s ...
- LeetCode(49)Group Anagrams
题目 Given an array of strings, group anagrams together. For example, given: ["eat", "t ...
- LeetCode(125) Valid Palindrome
题目 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ign ...
- LeetCode(22)Generate Parentheses
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
- LeetCode(65) Valid Number
题目 Validate if a given string is numeric. Some examples: "0" => true " 0.1 " ...
- Thinkphp入门 五 —模型 (49)
原文:Thinkphp入门 五 -模型 (49) [数据库操作model模型] model 模型 数据库操作 tp框架主要设计模式:MVC C:controller 控制器 shop/Li ...
随机推荐
- 剑指offer面试题4 替换空格(c)
- 16 Content Provider总结
第16天 Content Provider 一, 什么是Content Provider? 内容提供者 Android四大主件之一 :短信记录 通讯录 联系人 自定义 >Content Prov ...
- Android开发学习之路--Activity之Intent
窗外再次飘起了小雪,还有1周就过年了,2016年即将到来,来年不知道自己将身处何处,船到桥头自然直吧.还是继续学习吧,上次学习了Activity,那么如果是两个Activity之间,怎么从一个Acti ...
- gradle编译出错:Execution failed for task ':app:compileTestDebugJava'.
今天更新了android studio,从0.5.3升级到0.6.1版本,结果在IDE中编译时没有问题,但是在命令行时编译就会出现以下错误: :app:compileTestDebugJava FAI ...
- HMAC
Hash-based Message Authentication Code HMAC是IP安全里必须实现的MAC方案,并且其他Internet协议中(如SSL)也使用了HMAC.HMAC已作为NIS ...
- 物料事务处理interface与temp解析
MTL_TRANSACTIONS_INTERFACE MTL_TRANSACTIONS_INTERFACE is the interface point between non– Inventory ...
- Java-IO之ByteArrayOutputStream
ByteArrayOutputSTream是字节数组输出流,继承于OutputStream.ByteArrayOutputStream中的数据被写入到一个byte数组中,缓冲区会随着数据的不断写入而自 ...
- Android学习之Animation(二)
接着上次的View Animation动画,这次是Frame Animation.具体点来讲就是在Frame层面上进行变化的动画效果的设置.说白了就是定时更换"背景"图.来实现不同 ...
- UNIX环境高级编程——pthread_create的问题
linux 下常用的创建多线程函数pthread_create(pthread_t * thread , pthread_attr_t * attr , void *(*start_routine)( ...
- jdbc连接mysql加载驱动程序com.mysql.jdbc.Driver
在开发环境如eclipse,中加载指定数据库的驱动程序.需要下载MySQL支持JDBC的驱动程序mysql-connector-java-5.1.25-bin.jar. 而具体在Java程序中加载驱动 ...