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 有效括号的更多相关文章

  1. [LeetCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  2. [Leetcode] valid parentheses 有效括号对

    Given a string containing just the characters'(',')','{','}','['and']', determine if the input strin ...

  3. [LeetCode] 20. Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  4. [LeetCode] 20. Valid Parentheses 合法括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  5. [leetcode]20. Valid Parentheses有效括号序列

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  6. [LeetCode] Generate Parentheses 生成括号

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  7. [LintCode] Valid Parentheses 验证括号

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  8. LeetCode: Valid Parentheses 解题报告

    Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', det ...

  9. LeetCode 20 Valid Parentheses (括号匹配问题)

    题目链接 https://leetcode.com/problems/valid-parentheses/?tab=Description   Problem: 括号匹配问题. 使用栈,先进后出!   ...

随机推荐

  1. From COM to COM 侯捷 1998.06.12

    摘要: 本文簡介 C++ Object Model 和 Component Object Model 的基本概念,並引介四本書籍: 1. Inside The C++ Object Model 2. ...

  2. 蚂蚁金服HR电话面

    1.先自我介绍一下. 2.问到项目问题.(项目介绍,用到哪些技术,如何测试的.) 3.一些基础知识. a) list.map.set是继承了collection的吗?  List:1.可以允许重复的对 ...

  3. 微信小程序分列显示数据

    效果图 wxml代码 <view class='onehandle5'> <block wx:for="{{3}}" wx:for-index="k&q ...

  4. gitlab私服搭建

    1.什么是gitlab: gitlab 是一个用于仓库管理系统的开源项目,它以git为代码管理工具,因此代码可以管到它上边来.搭建私服gitlab可以方便我们有条不紊的管理我们的代码.闲话不说,上代码 ...

  5. IO模式和IO多路复用详解

    网络编程里常听到阻塞IO.非阻塞IO.同步IO.异步IO等概念,总听别人装13不如自己下来钻研一下.不过,搞清楚这些概念之前,还得先回顾一些基础的概念. 1 基础知识回顾 注意:咱们下面说的都是Lin ...

  6. Topoi 测验1301, 问题C: 1959: 解题 解题报告

    Topoi(一个经常会炸的网站) 本题提交链接 很久以前的题目了, 刚开了博客,来写一波题解 先看一波提交记录: 调了好几天QAQ 唉! 要是这些高手里有我估计直接 输出1 就AC了 算法 DFS + ...

  7. code和pre竟然有区别!!!!

    坑爹的,今天复习基础,本来写了个获取样式,为了显示出我的代码,才发现了 code和pre标签竟然是不同的. 1.用 JS 的代码注释风格来说 code 就是 // , <pre> 就是 / ...

  8. CodeForces - 186A-Comparing Strings

    Some dwarves that are finishing the StUDY (State University for Dwarven Youngsters) Bachelor courses ...

  9. 2017 ACM/ICPC Asia Regional Shenyang Online number number number

    题意:求n个斐波那契数列组合都无法得到的最小数字 解法: 1 我们先暴力的求出前面几个数字 2 然后再暴力的求递推 3 接着矩阵快速幂(没写错吧?) /*#include<bits/stdc++ ...

  10. 模拟虚拟的文件系统initrd/initramfs

    请看initramfs文件的以下解析: [root@ant-colonies boot]# ls config--.el6.x86_64 lost+found efi symvers--.el6.x8 ...