LeetCode OJ--Valid Parentheses
http://oj.leetcode.com/problems/valid-parentheses/
对栈的考察,看括号的使用方式是否合法。
class Solution {
public:
bool isValid(string s) {
if(s.empty() || s == "")
return true;
if(s.size()%!= )
return false;
int i = ;
stack<char> myStack;
while(i!=s.size())
{
if(s[i] == '(' || s[i] == '{' || s[i] == '[')
myStack.push(s[i]);
else
{
if(myStack.empty())
return false;
char chStackTop = myStack.top();
myStack.pop();
if(s[i]== ')' && chStackTop!= '(' || s[i]== '}' && chStackTop!= '{' ||s[i]== ']' && chStackTop!= '[' )
return false;
else if(s[i]!= ')' && s[i]!= '}' && s[i]!= ']')
return false;
}
i++;
}
if(myStack.empty())
return true;
else
return false;
}
};
LeetCode OJ--Valid Parentheses的更多相关文章
- [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] 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] 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 ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
随机推荐
- UEditor练习(JSP版)
下载1.4.3.3jsp版本的源码. <!DOCTYPE html> <html> <head> <meta charset="utf-8" ...
- Base64编码密钥时关于换行的几个问题。
在windows下一个javaweb应用,需要用http传递公钥pk.一般是String pk = BASE64ENCODER.encode(pkBytes);base64编码时,每76个字母就要换行 ...
- html中常见符号的代码表示
HTML中空格的集中代码表示: HTML中空格 不断行的空白(1个字符宽度) 半个空白(1个字符宽度) 一个空白(2个字符宽度) 窄空白(小于1个字符宽度) 其他常见的 ...
- urlrewrite地址重写实例
urlrewrite主要实现后天请求中的地址重写,防止被安全漏洞盲注入 http://tuckey.org/urlrewrite/ 下载最新的jar 下面是使用说明: 1.下载urlrewrite,官 ...
- ASIHTTPRequest简单学习
ASIHTTPRequest框架是优秀的第三方Objective-C的HTTP框架,支持Mac OS X和iOS下的HTTP开发. 一.ASIHTTPRequest框架的安装和配置 (1)首先要在项目 ...
- C语言实现链表及其操作
#include <stdio.h> #include <stdlib.h> //定义节点 typedef struct Node { int data; struct Nod ...
- MySql压缩版安装及避免1055错误和msvcp120.dll丢失
MySql压缩版安装及避免1055错误和msvcp120.dll丢失 MySQL压缩版的安装快速方便,5.7及最新的8版本安装方式大致相同. 在使用group by分组时,可能会遇到1055错误. 另 ...
- javascript 使用 load 和 unload 事件,解决浏览器打开和关闭时需要做的操作。
最近有一个业务,就是修改一个业务需要加上锁.也就是打开浏览器时,加锁.等用户操作完毕,关掉浏览器之后在把锁打开.一开始想问题很局限.只是想着,关闭浏览器解锁,刷新页面不做操作.然后就一直在找在调用un ...
- centos7 安装rabbitmq rabbitmq-c以及amqp扩展 详细篇
自己鼓捣了一晚上总算整明白了,有几个坑分享给小伙伴,希望能帮到你 前期准备 安装erlang 下载rpm包地址:https://github.com/rabbitmq/erlang-rpm (注意er ...
- Python文件指针与Python函数
文件内指针移动:f.seek() 强调:只有t模式下read(n),n代表字符的个数,除此以外都是以字节为单位. """ 文件内容:哈哈哈哈 呵呵呵呵 "&qu ...