leetcode 20

判断括号的顺序是否正确;

思路:用一个堆栈来存储符号序列,按照符号匹配规则进行堆栈操作;

   前括号一律入栈,后括号如果跟栈顶符号匹配,栈顶符号出栈如果,若不匹配则返回false;

   最后栈为空返回true,否则返回false;

代码如下:

 1 class Solution {
 2 public:
 3     bool isValid(string s) {
 4         int n = s.length();
 5         if(n%2 != 0)
 6         {
 7             return false;
 8         }
 9         vector<char> stack;
10         if(s[0] != ‘(‘ && s[0] != ‘[‘ && s[0] != ‘{‘)
11         {
12             return false;
13         }
14         else
15         {
16             stack.push_back(s[0]);
17         }
18         int j = 1;
19         for(int i = 1; i < n; ++i)
20         {
21             if(s[i] == ‘(‘ || s[i] == ‘[‘ || s[i] == ‘{‘)
22             {
23                 stack.push_back(s[i]);
24             }
25             else if(s[i] == ‘)‘)
26             {
27                 if(stack.back() == ‘(‘)
28                 {
29                     stack.pop_back();
30                 }
31                 else
32                 {
33                     return false;
34                 }
35             }
36             else if(s[i] == ‘]‘)
37             {
38                 if(stack.back() == ‘[‘)
39                 {
40                     stack.pop_back();
41                 }
42                 else
43                 {
44                     return false;
45                 }
46             }
47             else if(s[i] == ‘}‘)
48             {
49                 if(stack.back() == ‘{‘)
50                 {
51                     stack.pop_back();
52                 }
53                 else
54                 {
55                     return false;
56                 }
57             }
58         }
59         if(stack.empty())
60         {
61             return true;
62         }
63         else
64         {
65             return false;
66         }
67     }
68 };
时间: 2024-08-09 08:11:44

leetcode 20的相关文章

[LeetCode]20 Valid Parentheses 有效的括号

[LeetCode]20 Valid Parentheses 有效的括号 Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brac

Valid Parentheses [LeetCode 20]

1- 问题描述 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 20 Valid Parentheses stack的应用

判断括号是否合法 1.用stack存入左括号“([{”,遇到相应的右括号“)]}”就退栈 2.判断stack是否为空,可以判断是否合法 1 class Solution { 2 public: 3 bool isValid(string s) { 4 stack<char> sc; 5 char in[] ="([{"; 6 char out[] =")]}"; 7 for(string::size_type i = 0; i < s.size()

leetCode 20. Valid Parentheses 字符串

20. 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 "(]&quo

leetcode 20 简单括号匹配

栈的运用 1 class Solution { 2 public: 3 bool isValid(string s) { 4 stack<char>The_Stack; 5 int i=0; 6 The_Stack.push('#'); 7 while(i<s.size()) { 8 if((s[i]==')'&&The_Stack.top()=='(')||(s[i]==']'&&The_Stack.top()=='[')||(s[i]=='}'&

[leetcode] 20. 有效的括号

20. 有效的括号 括号匹配,用栈即可.高中时第一次学栈后做的第一个题.. class Solution { public boolean isValid(String s) { Stack<Character> characterStack = new Stack<>(); for (int i = 0; i < s.length(); i++) { switch (s.charAt(i)) { case '(': case '[': case '{': character

LeetCode - 20. Valid Parentheses(0ms)

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct

19.1.29 [LeetCode 20] Valid Parentheses

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct

Leetcode 20.有效的括号 js

---恢复内容开始--- 题目描述: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效.有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认为是有效字符串. 示例 1: 输入: "()"输出: true 示例 2: 输入: "()[]{}"输出: true 示例 3: 输入: "(]"输出: false 示例 4: 输入: "([)]&quo