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.

括号匹配的问题,使用栈解决。(注意,访问栈元素时要先判断栈是否为空)

C++实现代码如下:

#include<iostream>
#include<string>
#include<stack>
using namespace std;

class Solution {
public:
    bool isValid(string s) {
        size_t index=0;
        stack<char> ss;
        while(index<s.length())
        {
            char c;
            switch(s[index])
            {
            case '(' :
            case '[' :
            case '{' :
                ss.push(s[index]);
                break;
            case ')' :
                if(ss.empty())
                    return false;
                c=ss.top();
                if(c!='(')
                    return false;
                ss.pop();
                break;
            case ']' :
                if(ss.empty())
                    return false;
                c=ss.top();
                if(c!='[')
                    return false;
                ss.pop();
                break;
            case '}' :
                if(ss.empty())
                    return false;
                c=ss.top();
                if(c!='{')
                    return false;
                ss.pop();
                break;
            default:
                return false;
            }
            index++;
        }
        if(index==s.length()&&ss.empty())
            return true;
        return false;
    }
};

int main()
{
    string str="]";
    Solution s;
    cout<<s.isValid(str)<<endl;
}

运行结果:

#include<string>
#include<iostream>
#include<stack>
using namespace std;

class Solution
{
public:
    bool isValid(string s)
    {
        stack<char> st;
        int i;
        for(i=0;i<(int)s.size();i++)
        {
            switch(s[i])
            {
            case '(':
            case '[':
            case '{':
                st.push(s[i]);
                break;
            case ')':
                if(st.empty()||st.top()!='(')
                    return false;
                st.pop();
                break;
            case ']':
                if(st.empty()||st.top()!='[')
                    return false;
                st.pop();
                break;
            case '}':
                if(st.empty()||st.top()!='{')
                    return false;
                st.pop();
                break;
            }
        }
        if(st.empty())
            return true;
        else
            return false;
    }
};

int main()
{
    Solution s;
    string str="((()))";
    cout<<s.isValid(str)<<endl;
}

  

时间: 2024-12-05 00:40:46

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

LeetCode 32 Longest Valid Parentheses(最长有效括号)(*)

翻译 给定一个仅仅包含"("或")"的字符串,找到其中最长有效括号子集的长度. 对于"(()",它的最长有效括号子集是"()",长度为2. 另一个例子")()())",它的最长有效括号子集是"()()",其长度是4. 原文 Given a string containing just the characters '(' and ')', find the length of the l

[LeetCode]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 i

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

LeetCode 20 Valid Parentheses(有效的括号)

翻译 给定一个只包含'(', ')', '{', '}', '[' 和']'的字符串,判断这个输入的字符串是否是有效的. 括号必须在正确的形式下闭合,"()" 和"()[]{}" 是有效的,但是 "(]" 和"([)]" 则不是. 原文 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the

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

UVa 673 Parentheses Balance (栈)

673 - Parentheses Balance Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=103&page=show_problem&problem=614 You are given a string consisting of parentheses () and []. A string of thi

[LeetCode] Valid Parenthesis String 验证括号字符串

Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules: Any left parenthesis '(' must have a corresponding right parenthesi