LeetCode OJ: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.

简单的堆栈问题,代码如下:

 1 class Solution {
 2 public:
 3     bool isValid(string s) {
 4         if(!s.size())
 5             return true;
 6         char c;
 7         stack<char> stk;
 8         for(int i = 0; i < s.size(); ++i){
 9             if(s[i] == ‘[‘ || s[i] == ‘{‘ || s[i] == ‘(‘){
10                 stk.push(s[i]);
11                 continue;
12             }else if(s[i] == ‘]‘){
13                 if(stk.empty())
14                     return false;
15                 c = stk.top();
16                 stk.pop();
17                 if(c != ‘[‘)
18                     return false;
19             }else if(s[i] == ‘}‘){
20                 if(stk.empty())
21                     return false;
22                 c = stk.top();
23                 stk.pop();
24                 if(c != ‘{‘)
25                     return false;
26             }else{
27                 if(stk.empty())
28                     return false;
29                 c = stk.top();
30                 stk.pop();
31                 if(c != ‘(‘)
32                     return false;
33             }
34         }
35         if(stk.empty())
36             return true;
37         return false;
38     }
39 };
时间: 2024-12-30 15:30:09

LeetCode OJ:Valid Parentheses(有效括号)的相关文章

[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) {

[LeetCode] 22. 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: "((()))", "(()())", "(())()", "()(())", "()()()" 给一个数字n,

LeetCode:Valid Parentheses - 合理的括号搭配

1.题目名称 Valid Parentheses(合理的括号搭配) 2.题目地址 https://leetcode.com/problems/valid-parentheses/ 3.题目内容 英文:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the

[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

[LeetCode] Longest Valid Parentheses 最长有效括号

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

[LeetCode]Longest Valid Parentheses, 解题报告

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example i

LeetCode: Longest Valid Parentheses [031]

0.下载安装Opencv,当前版本为249. 1.下载Python,当前OPencv版本为249,不过其支持的最新版本的Python为2.7,所以可以下载276版本. 2.下载numpy,开始我使用了1.6,没有通过,错误如图.下载了最新的1.8.1版本. 3.将Opencv安装目录下opencv\build\python\2.7\x86中的cv2.pyd复制到python安装目录Lib\site-packages下. 4.找到opencv源文件内的draw.py运行. LeetCode: Lo

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 20.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