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: 括号匹配问题. 使用栈,先进后出! ...
随机推荐
- hdu1071
#include <iostream> #include <stdio.h> using namespace std; int main() { int t; double x ...
- hdu1056
#include <cstdio> void main(){ double length; double l[300]; l[1] = 1.0/2; int i; for (i = 2;; ...
- 释放License命令
lmutil_x86.exe lmremove -c 1055@Vappcloud-WinS acfd Administrator VappCloud_Win7a.vancloud-corp.com ...
- Ping++中的AlipaySDK和AlicloudUTDID冲突解决方案
今天维护一个老项目发现阿里框架冲突 问题截图: 解决方案: 去阿里文档中心 https://docs.open.alipay.com/54/104509 重新下载没有UTDID冲突的库 下载SDK解压 ...
- 【转-mysql索引失效的几种情形】
索引并不是时时都会生效的,比如以下几种情况,将导致索引失效: 1.如果条件中有or,即使其中有条件带索引也不会使用(这也是为什么尽量少用or的原因) 注意:要想使用or,又想让索引生效,只能将or条件 ...
- 【转】webservice
一.WebService概念 Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它 ...
- C# 外观模式
外观模式(Facade Pattern) 介绍 定义: 为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用.使用外观模式时,创建了一个统一的类,用来 ...
- Note: Bimodal Content Defined Chunking for Backup Streams
CDC算法给出了一个chunk的大小的最小值.最大值.平均值的界定. Method Using chunk existence information breaking-apart algorithm ...
- 分别使用ES5和ES6进行数组去重以及注意事项
ES6,ES5数组去重 使用Es6进行数组去重 var arr = [false, true, undefined, null, NaN, 0, 1, {}, {}, 'a', 'a', NaN]; ...
- oracle基本命令
1.首先,创建(新)用户: create user username identified by password; username:新用户名的用户名 password: 新用户的密码也可以不创建新 ...