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: 括号匹配问题. 使用栈,先进后出! ...
随机推荐
- <转>哥舒意:关于米奇的三个真相 (米奇·阿尔博姆)
http://book.douban.com/review/6436827/ <时光守护者>的故事也不算复杂,虽然有三条线同时进行,但是三条线的情节都条理分明,而且这次他使用了类似于奇 ...
- Sharepoint2013商务智能学习笔记之部署AdventureWorksDW2012数据库(三)
AdventureWorksDW2012是sql server2012的样本数据库,后面做商务智能Demo会用到,所以需要下载并安装到sql server2012上,下载地址 第一步,下载数据库 第二 ...
- Asp.Net 之 Web.config 配置文件详解 -转
在asp.net中配置文件名一般默认是web.config.每个web.config文件都是基于XML的文本文件,并且可以保存到Web应用程序中的任何目录中.在发布Web应用程序时web.config ...
- Vue实现添加、删除、关键字查询
从今天开始,将不定期更新关于 Vue 的学习以及各种方法的使用,好了,下面就开始吧 Vue的实例创建首先需要我们引入一个vue.js(也可以在本地npm安装vue,我为了省事就...),然后在HTML ...
- Syslog+Fluentd+InfluxDB日志收集系统搭建
环境配置 节点 配置 类型 操作系统 Sched 2G 2CPU 50GB ens3=>192.168.200.11 KVM虚拟机 CentOS 7 Nova 4G 2CPU 50GB ens3 ...
- go语言实战教程:项目文件配置和项目初始化运行
在上节内容中,我们已经搭建了实战项目框架,并将实战项目开发所需要的静态资源文件进行了导入.在本节内容中,我们将讲解如何通过相关的配置,并初始化运行项目. conf配置文件读取配置信息 我们前面说过,使 ...
- 【转载】【爬坑记录】hyperledger caliper 性能测试工具使用的一些问题记录
原文: https://blog.csdn.net/raogeeg/article/details/82752613 安装方法详见:https://github.com/hyperledger/cal ...
- [Xcode 实际操作]三、视图控制器-(11)在Storyboard中使用表格控件
目录:[Swift]Xcode实际操作 本文将演示表格控件在故事板中的使用. 点击[显示或隐藏检查器按钮],再界面右侧打开检查器面板. 在控制器根视图上点击鼠标,以选择该根视图. 现在往根视图中添加一 ...
- Miller-Rabin素性测试|Pollard's Rho算法
Miller-Rabin 素性测试 Miller-Rabin 素数测试 一本通上的M-R不透彻啊~ Miller-Rabin是利用随机化算法判断一个数是合数还是素数. 首先,如果一个数N是素数,那么他 ...
- Error: Attribute application@allowBackup value=(false) from AndroidManifest.xml:14:7-34 is also present at [:react-native-qq] AndroidManifest.xml:14:18-44 value=(true).
解决方法: 修改文件: 在manifest标签中添加 xmlns:tools="http://schemas.android.com/tools" 在application标签中添 ...