[LeetCode 题解]: 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.
题解: 典型的STL stack 应用。注意边界条件。
class Solution {
public:
bool isValid(string s) {
stack<char> st;
if(s.size()<= || s.size()%) return false;
for(int i=;i<s.size();i++)
{
if(s[i]=='(' || s[i]=='[' || s[i]=='{') st.push(s[i]);
else if(st.empty())
{
return false;
}
else
{
if( (s[i]==')' && st.top()=='(') ||
(s[i]==']' && st.top()=='[') ||
(s[i]=='}' && st.top()=='{') )
st.pop();
else
return false;
}
}
if(!st.empty()) return false;
else return true;
}
};
转载请注明出处: http://www.cnblogs.com/double-win/ 谢谢!
[LeetCode 题解]: Valid Parentheses的更多相关文章
- leetcode 题解 || Valid Parentheses 问题
problem: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [LeetCode] Longest Valid Parentheses 解题思路
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [Leetcode] longest valid parentheses 最长的有效括号
Given a string containing just the characters'('and')', find the length of the longest valid (well-f ...
- [LeetCode] Longest Valid Parentheses -- 挂动态规划羊头卖stack的狗肉
(Version 1.3) 这题在LeetCode上的标签比较有欺骗性,虽然标签写着有DP,但是实际上根本不需要使用动态规划,相反的,使用动态规划反而会在LeetCode OJ上面超时.这题正确的做法 ...
- [LeetCode] 20. Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [LeetCode] Longest Valid Parentheses
第一种方法,用栈实现,最容易想到,也比较容易实现,每次碰到‘)’时update max_len,由于要保存之前的‘(’的index,所以space complexity 是O(n) // 使用栈,时间 ...
- [LeetCode] Longest Valid Parentheses 动态规划
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [leetcode]20. Valid Parentheses有效括号序列
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
随机推荐
- Python小知识点(2)
1.简述Python的运行过程 在说这个问题之前,我们先来说两个概念,PyCodeObject和pyc文件. 我们在硬盘上看到的pyc自然不必多说,而其实PyCodeObject则是Python编译器 ...
- 爬虫——回顾HTTP 协议
一.HTTP协议 1.官方概念: HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输超文 ...
- Linux下安装php的memcached扩展(memcache的客户端)
php的扩展memcache,不支持cas,所以我们要装memcached扩展,memcached扩展是基于libmemcached,所以要先安装libmemcached 一.下载软件 1.libme ...
- C#读取Excel技术概览 (2)
5.自定义SDK,使用xmlReader文件流式处理 第四章节中,总是感觉用别人的工具要受制于人.既然我 们知道了Excel的存储方式,问题便转换成从xml中取出数据,然后放入内存得到我们想要的东西, ...
- 详解C++右值引用
C++0x标准出来很长时间了,引入了很多牛逼的特性[1].其中一个便是右值引用,Thomas Becker的文章[2]很全面的介绍了这个特性,读后有如醍醐灌顶,翻译在此以便深入理解. 目录 概述 mo ...
- 刷题向》一道简单的思路题BZOJ1800(EASY+)
这道题其实并不难,主要原因是数据范围很小,当然数据如果大来也可以优化,但重点是在做的时候用的思路很通用, 所以本题是一道思想题(当然思想也不难) 标题里的“+”体现在一些边界处理中. 直接甩题目 De ...
- $(window).load()和$(document).ready()
一.前言 我们在编写前端代码的js文件时,往往是会先写一个$(function(){}),然后才会在大括号里面继续写我们自己的代码.当时并不能理解为什么要添加这样一个东西,只是把它当做一个标签一样添加 ...
- 使用CocoaPods卡在了"pod setup"界面的解决办法
http://blog.csdn.net/samoy/article/details/51956799 有时候,我们在执行pod install或pod search命令时,会在终端偶现卡在’ ...
- Canny效果
- &&与||的短路运算
在谈&&和||两个运算符的短路运算之前,先看一段程序: #include <stdio.h> int main() { , para2 = , para3 = , para ...