[string]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.

class Solution {
public:
    bool isValid(string s) {
        stack<char> stk;
        int size = s.size();
        for(int i=0;i<size;i++){
            if(s[i]==‘(‘||s[i]==‘[‘||s[i]==‘{‘){
                stk.push(s[i]);
            }else{
                if(stk.empty()){
                    return false;
                }
                char topCh = stk.top();
                if((s[i]==‘]‘ && topCh==‘[‘ )||
                   (s[i]==‘)‘ && topCh==‘(‘ )||
                   (s[i]==‘}‘ && topCh==‘{‘ )
                   ){
                      stk.pop();
                   }else{
                       return false;
                   }
            }
        }
        return stk.empty()? true:false;
    }
};
时间: 2024-08-08 09:28:36

[string]Valid Parentheses的相关文章

leetcode_20题——Valid Parentheses(string,stack堆栈)

Valid Parentheses Total Accepted: 47887 Total Submissions: 180550My Submissions Question Solution Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the co

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

32. Longest Valid Parentheses Problem's Link ---------------------------------------------------------------------------- Mean: 给定一个由'('和')'组成的字符串,求最长连续匹配子串长度. analyse: 定义一个stack<pair<char,int>>类型的stack. 遇到'('进栈; 遇到')'需要分两种情况讨论: 栈顶元素为'(',正好匹配,

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: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之“动态规划”: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 e

LeetCode: Longest Valid Parentheses O(n)时间 O(1)空间

题目: 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

leetcode第31题--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 &

每日算法之二十八: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 &