LeetCode Valid Parentheses 有效括号

 1 class Solution {
 2 public:
 3 void push(char c){        //插入结点
 4     struct node *n=new struct node;
 5     n->nex=0;
 6     n->ch=c;
 7     n->pre=last;
 8     last->nex=n;
 9     last=last->nex;
10 }
11 bool jud(char c){        //判断
12     struct node *m;
13     if(c==‘]‘)
14         c=‘[‘;
15     else if(c==‘}‘)
16         c=‘{‘;
17     else if(c==‘)‘)
18         c=‘(‘;
19     if(last->ch==c&&first.nex!=0){
20         m=last;
21         last=last->pre;
22         delete m;
23         last->nex=0;
24         return 1;
25     }
26     else
27         return 0;
28 }
29 bool isValid(string s) {    //主判断函数
30     char *p=&s[0];
31     while(*p!=‘\0‘){
32         if(*p==‘[‘ || *p==‘{‘ || *p==‘(‘)
33             push(*p);
34         else if(*p==‘]‘ || *p==‘}‘ || *p==‘)‘){
35             if(jud(*p)==false)    //判断错误
36                 return 0;
37         }
38         p++;
39     }
40     if(last==&first)
41         return 1;
42     else
43         return 0;
44 }
45 private:
46 struct node{        //结构体
47     struct node *pre;
48     struct node *nex;
49     char ch;
50 }first={0,0,0};
51 struct node *last=&first;
52
53 };

题意:判断括号是否成对的出现,并且是合法的,类似判断一个算术式子中的括号有没有错一样。过滤掉非3种括号的其他字符。

思路:因为不知道这个string到底多大,可能很大,可能很小,所以我选择用盏(链表)来解决。一旦有不成对出现的,立刻就可以返回了。我这里用多两个函数将功能模块化,一个用于进盏,一个用于转化括号为另一半并判断。正常情况下结束时盏中应该空(我用last是否为链表头first来判断)。

注意:此题用STL和string类来解决应该更简单。代码量也会减少很多。

时间: 2024-07-30 10:08:01

LeetCode Valid Parentheses 有效括号的相关文章

[Leetcode] valid parentheses 有效括号对

Given a string containing just the characters'(',')','{','}','['and']', determine if the input string is valid. The brackets must close in the correct order,"()"and"()[]{}"are all valid but"(]"and"([)]"are not. 题意:给

[LeetCode] Valid Parentheses 验证括号

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"

Leetcode: Valid Parentheses 有效的括号匹配

Valid Parentheses: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]"

LeetCode: Valid Parentheses [020]

[题目] Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]

LeetCode——Valid Parentheses

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"

[LeetCode] Generate Parentheses 生成括号

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 在LeetCo

LeetCode: Valid Parentheses 解题报告

Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" a

[LeetCode]35. Valid Parentheses有效括号

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"

【LeetCode】Valid Parentheses合法括号

给定一个仅包含 '('.')'.'{'.'}'.'['.']'的字符串,确定输入的字符串是否合法. e.g. "()"."()[]{}"."[()]([]({}))" 是合法的,而"(]"."([)]" 是不合法的. 使用栈stack C++实现: 1 bool isValid(string s) { 2 stack<char> stack; 3 for (char &c : s) {