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 ...
随机推荐
- TraceView性能分析工具介绍
一.TraceView简介 TraceView是AndroidSDK里面自带的工具,用于对Android的应用程序以及Framework层的代码进行性能分析. TraceView是图形化的工具,最终它 ...
- UNIX网络编程——epoll 的accept , read, write(重要)
在一个非阻塞的socket上调用read/write函数,返回EAGAIN或者EWOULDBLOCK(注:EAGAIN就是EWOULDBLOCK). 从字面上看,意思是: EAGAIN: 再试一次 E ...
- 让 Google Test 出错时断点
Google Test 缺省是出错退出. 如果最后的出错行在系统库中,那就没什么帮助. 如果是调试运行,直接退出根本就不知道哪里出错了. 后来添加了一个运行参数: --gtest_break_on_f ...
- Java-IO之BufferedReader(字符缓冲输入流)
BufferedReader是缓冲字符输入流,继承于Reader,BufferedReader的作用是为其他字符输入流添加一些缓冲功能. BufferedReader主要的函数列表: Buffered ...
- 常用js总结
通过radio来显示/隐藏一个div <div id="ArbAcei" > <br/> 有无不良反应停药 <input type="rad ...
- LPSTR、LPWSTR、LPCSTR、LPCWSTR、LPTSTR、LPCTSTR的区分与转化
首先在编译程序时经常会遇到这种问题: 错误 1 error C2664: "CWnd::MessageBoxW": 不能将参数 1 从"const char [3]&qu ...
- Cocos2D:塔防游戏制作之旅(十三)
让我们看一下Waves.plist文件,你将注意到它包含了3个数组.每一个数组表示一波攻击,也就是一组敌人一起到达闹事.第一个数组包含6个字典.每一个字典定义1个敌人. 在本次教程中,字典只存储敌人应 ...
- BAT有增有减 互联网2015校园…
又到一年开学季,也是毕业生开始被各种招聘.宣讲所围绕的时节. 在众多行业中,互联网在过往几年,也属于较热门的第一梯队之中.不过,在2015年的经济形势下,大家不由地疑问,互联网企业的招聘还会持续吗? ...
- Uva - 506 - System Dependencies
模拟题,注意显示安装和隐式安装,显示安装的必须显示显示删除.把名字转化为整数维护.其他注意都注释了.输入稍微多一下,题目不是很麻烦. AC代码: #include <iostream> # ...
- Chapter 2 User Authentication, Authorization, and Security(6):服务器权限授予粒度
原文出处:http://blog.csdn.net/dba_huangzj/article/details/38867489,专题目录:http://blog.csdn.net/dba_huangzj ...