20 Valid Parentheses(匹配括号)
题目意思:判断一个字符串(){}[]是否符合
思路:用栈
ps:实习一个多月了,代码也刷不动了,状态真不是一般的差
class Solution {
public:
bool isValid(string s) {
if(s==""||s.size()%==)
return false;
stack<char> mystack;
for(int i=;i<s.length();++i){
if(s[i]=='['||s[i]=='('||s[i]=='{'){
mystack.push(s[i]);
continue;
}
else{
if(s[i]){
if(mystack.empty())
return false;
}
switch(s[i]){
case ']':
if(mystack.top()!='[')
return false;
else
mystack.pop();
break;
case ')':
if(mystack.top()!='(')
return false;
else mystack.pop();
break;
case '}':
if(mystack.top()!='{')
return false;
else mystack.pop();
break;
}
}
}
if(mystack.empty())
return true;
return false;
}
};
20 Valid Parentheses(匹配括号)的更多相关文章
- [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] 20. Valid Parentheses 合法括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode 20 Valid Parentheses (括号匹配问题)
题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description Problem: 括号匹配问题. 使用栈,先进后出! ...
- 20. Valid Parentheses检验括号字符串的有效性
[抄题]: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- LeetCode解题笔记 - 20. Valid Parentheses
这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶 ...
- 刷题20. Valid Parentheses
一.题目说明 这个题目是20. Valid Parentheses,简单来说就是括号匹配.在学数据结构的时候,用栈可以解决.题目难度是Medium. 二.我的解答 栈涉及的内容不多,push.pop. ...
随机推荐
- 基于TCP协议的服务器(多线程)
import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.IOException; impo ...
- Python运行机制
闲来无事,简单画了一下Python的运行机制,纯属娱乐:
- 近5年133个Java面试问题列表
Java 面试随着时间的改变而改变.在过去的日子里,当你知道 String 和 StringBuilder 的区别就能让你直接进入第二轮面试,但是现在问题变得越来越高级,面试官问的问题也更深入. 在我 ...
- 1 weekend110的复习 + hadoop中的序列化机制 + 流量求和mr程序开发
以上是,weekend110的yarn的job提交流程源码分析的复习总结 下面呢,来讲weekend110的hadoop中的序列化机制 1363157985066 13726230503 ...
- php 燕十八 观察者模式代码例子
<?php class user implements SplSubject { public $lognum; public $hobby; protected $observers=null ...
- 循环中continue和break的区别
continue是指跳出本次循环 进入下次循环 再来一次 break是指循环结束 再也不来了 可以用一个广告来解释这个区别 这是continue 房:Hi 郭:是你啊 房:我订的书到了吗 房:啊!对了 ...
- MariaDB设置主从复制[转载]
3. MariaDB设置主从复制 标签: mariadbMySQL主从复制 翻译人员: 铁锚 翻译日期: 2013年12月25日 原文链接: Setting Up Replication 主从复制 ...
- 四种可变交流swap方法
1.void swap(int &x, int &y){ int temp=x; x=y; y=temp; } 2.void swap(int &x, int &y){ ...
- linux解压缩命令
1.tar -cvf /data/sc2.tar /data (只打包,不压缩) 把/data下的文件打包成 sc.tar 上面两个都是绝对路径噢 tar -zcvf /data/sc2.tar.g ...
- use_virtual_func_without_pointer_left
#include <oistream> using namespace std; class A { public: void foo() { func(); } virtual void ...