LeetCode Valid Parentheses 有效括号
class Solution {
public:
void push(char c){ //插入结点
struct node *n=new struct node;
n->nex=;
n->ch=c;
n->pre=last;
last->nex=n;
last=last->nex;
}
bool jud(char c){ //判断
struct node *m;
if(c==']')
c='[';
else if(c=='}')
c='{';
else if(c==')')
c='(';
if(last->ch==c&&first.nex!=){
m=last;
last=last->pre;
delete m;
last->nex=;
return ;
}
else
return ;
}
bool isValid(string s) { //主判断函数
char *p=&s[];
while(*p!='\0'){
if(*p=='[' || *p=='{' || *p=='(')
push(*p);
else if(*p==']' || *p=='}' || *p==')'){
if(jud(*p)==false) //判断错误
return ;
}
p++;
}
if(last==&first)
return ;
else
return ;
}
private:
struct node{ //结构体
struct node *pre;
struct node *nex;
char ch;
}first={,,};
struct node *last=&first;
};
题意:判断括号是否成对的出现,并且是合法的,类似判断一个算术式子中的括号有没有错一样。过滤掉非3种括号的其他字符。
思路:因为不知道这个string到底多大,可能很大,可能很小,所以我选择用盏(链表)来解决。一旦有不成对出现的,立刻就可以返回了。我这里用多两个函数将功能模块化,一个用于进盏,一个用于转化括号为另一半并判断。正常情况下结束时盏中应该空(我用last是否为链表头first来判断)。
注意:此题用STL和string类来解决应该更简单。代码量也会减少很多。
LeetCode Valid Parentheses 有效括号的更多相关文章
- [LeetCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- [Leetcode] valid parentheses 有效括号对
Given a string containing just the characters'(',')','{','}','['and']', determine if the input strin ...
- [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] Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [LintCode] Valid Parentheses 验证括号
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- LeetCode: Valid Parentheses 解题报告
Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...
- LeetCode 20 Valid Parentheses (括号匹配问题)
题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description Problem: 括号匹配问题. 使用栈,先进后出! ...
随机推荐
- Window Live Writer在Win7下安装提示错误“OnCatalogResult:0x80190194”
1.在C:\Users\All Users\Microsoft\WLSetup\Logs下(All Users默认是隐藏文件夹),有两个Window Live的安装日记记录文件,打开第一个找到ERRO ...
- js注册
ScriptManager.RegisterStartupScript(this, GetType(), "", "alert('修改失败!')", true) ...
- 前端中的事件循环eventloop机制
我们知道 js 是单线程执行的,那么异步的代码 js 是怎么处理的呢?例如下面的代码是如何进行输出的: console.log(1); setTimeout(function() { console. ...
- ABC118D(DP,完全背包,贪心)
#include<bits/stdc++.h>using namespace std;int cnt[10]={0,2,5,5,4,5,6,3,7,6};int dp[10007];int ...
- 利用CSS制作脸书
很多网站都支持图片上的头像框识别,鼠标在头像框处,会提示一些人物信息. 这次就利用CSS实现这样一个功能: div处主要包括两部分,一部分是图片:另一部分是链接以及脸框 <div class=& ...
- App裂变活动多种玩法解析
移动互联网时代,流量为王.在App获取流量的过程中,有资金的砸广告,没资金的铺渠道,但是不管你有钱没钱,社交平台都是必须重点争夺的流量阵地. 毕竟,截至2018年底,微信及WeChat的合并月活跃账户 ...
- C#网络编程学习(4)---Socket Tcp进阶之 使用异步循环接收客户端连接和信息
1.方法介绍 BeginAccept(AsyncCallback callback, object state); 异步开始监听客户端连接. callback为一个委托,在成功接收客户端连接时调用委托 ...
- List、Set
List.Set List.Set List.Set List.Set List.Set List.Set List.Set
- AT2045 Salvage Robots
传送门 这个题只要想到移动机器人和移动出口是等价的就好做了 考虑设\(f[i][j][k][t]\)为最远向左移动\(i\),向右移动\(j\),向上移动\(k\),向下移动\(t\),这个矩形内最多 ...
- shiro 重定向 后 带有 sessionId 的 解决 办法
http://blog.csdn.net/aofavx/article/details/51701012