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:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.

Note that an empty string is also considered valid.

Example 1:

Input: "()"
Output: true

Example 2:

Input: "()[]{}"
Output: true

Example 3:

Input: "(]"
Output: false

Example 4:

Input: "([)]"
Output: false

Example 5:

Input: "{[]}"
Output: true
 1 class Solution {
 2 public:
 3     bool isValid(string s) {
 4         int l = s.length();
 5         if(l == 0) {
 6             return true;
 7         }
 8
 9         stack<char> st;
10         for(int i=0; i<l; i++) {
11             if(s[i] == ‘(‘ || s[i] == ‘[‘ || s[i] == ‘{‘) {
12                 st.push(s[i]);
13             }
14             else if(st.size() == 0) {
15                 return false;
16             }
17             else {
18                 if(s[i] == ‘)‘) {
19                     if(st.top() == ‘(‘) {
20                         st.pop();
21                     }
22                     else {
23                         return false;
24                     }
25                 }
26                 else if(s[i] == ‘]‘) {
27                     if(st.top() == ‘[‘) {
28                         st.pop();
29                     }
30                     else {
31                         return false;
32                     }
33                 }
34                 else {
35                     if(st.top() == ‘{‘) {
36                         st.pop();
37                     }
38                     else {
39                         return false;
40                     }
41                 }
42             }
43         }
44         if(st.size() == 0) {
45             return true;
46         }
47         else {
48             return false;
49         }
50     }
51 };

原文地址:https://www.cnblogs.com/zlian2016/p/9382167.html

时间: 2024-11-05 01:36:18

LeetCode - 20. Valid Parentheses(0ms)的相关文章

[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 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 (有效的括号) 解题思路和方法

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 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 "(]" and "([)]"

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

class Solution { public: bool isValid(string s) { stack<char> parentheses; for (int i = 0; i < s.size(); ++i) { if (s[i] == '(' || s[i] == '[' || s[i] == '{') parentheses.push(s[i]); else { if (parentheses.empty()) return false; if (s[i] == ')' &

LeetCode #20 Valid Parentheses (E)

[Problem] 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()

Java [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 "(]" and "([)