20. Valid Parentheses(C++)

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.

Solution :
 1 class Solution {
 2 public:
 3     bool isValid(string s) {
 4         if(s.size()<2) return false;
 5         stack<char> stk;
 6         for(int i=0;i<s.size();i++){
 7             if((s[i]==‘(‘)||(s[i]==‘[‘)||(s[i]==‘{‘)) stk.push(s[i]);
 8             if((s[i]==‘}‘)||(s[i]==‘]‘)||(s[i]==‘)‘)){
 9                 if(stk.empty()) return false;
10                 char c=stk.top();
11                 stk.pop();
12                 switch (s[i]){
13                     case ‘}‘: if(c!=‘{‘) return false; break;
14                     case ‘)‘: if(c!=‘(‘) return false; break;
15                     case ‘]‘: if(c!=‘[‘) return false; break;
16                 }
17             }
18         }
19         if(!stk.empty()) return false;
20         return true;
21     }
22 };

better solution:

 1 class Solution {
 2 public:
 3     bool isValid(string s) {
 4         stack<char> paren;
 5         for (char& c : s) {
 6             switch (c) {
 7                 case ‘(‘:
 8                 case ‘{‘:
 9                 case ‘[‘: paren.push(c); break;
10                 case ‘)‘: if (paren.empty() || paren.top()!=‘(‘) return false; else paren.pop(); break;
11                 case ‘}‘: if (paren.empty() || paren.top()!=‘{‘) return false; else paren.pop(); break;
12                 case ‘]‘: if (paren.empty() || paren.top()!=‘[‘) return false; else paren.pop(); break;
13                 default: ; // pass
14             }
15         }
16         return paren.empty() ;
17     }
18 };
时间: 2024-10-14 02:47:35

20. Valid Parentheses(C++)的相关文章

20. Valid Parentheses【leetcode】

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. 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. Valid Parentheses

这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶段性查看,一定能对自己有帮助. 这是我做的第一题,所以记录也从这题开始,之后尽力以简短的说明,描述出思路,方便以后能回顾到简介明了的记录. 20. Valid Parentheses Given a string containing just the characters '(', ')', '{

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 "(]"

20. Valid Parentheses(js)

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

[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

刷题20. Valid Parentheses

一.题目说明 这个题目是20. Valid Parentheses,简单来说就是括号匹配.在学数据结构的时候,用栈可以解决.题目难度是Medium. 二.我的解答 栈涉及的内容不多,push.pop.top,. 我总共提交了3次: 第1次:Runtime Error,错误原因在于pop的时候,未判断栈是否为空. 第2次:Wrong Answer,这个是"眼大"疏忽导致的,我写的时候只考虑了()[]未考虑{}. 第3次:终于正确了,性能还可以: Runtime: 0 ms, faster

[Leetcode][Python]20: Valid Parentheses

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 20: Valid Parentheseshttps://oj.leetcode.com/problems/valid-parentheses/ Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.T

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

20. Valid Parentheses做题报告

题目链接: Valid Parentheses 题目大意: 判断字符串s的字符是否满足符号匹配 做题报告: (1)该题涉及的算法与数据结构 栈,哈希表 (2)自己的解答思路+代码+分析时间和空间复杂度 思路: 栈先入后出特点,若遇到左括号入栈,遇到右括号时将对应栈顶左括号出栈,则遍历完所有括号后 stack 仍然为空则表示满足符号匹配,输出true,否则输出false 代码: import java.util.Stack; class Solution { public boolean isVa