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

【分析】

这是栈的应用。

注意一下情况:

(1) [])   左右括号数目不匹配,左括号少,右括号多,因此出栈时需判断栈是否空 if(!brackets.empty())

(2)  ((    左右括号数目不匹配,左括号多,右括号少, 因此遍历完后
需判断栈是否空                           

【代码】

/*********************************
*   日期:2015-01-23
*   作者:SJF0115
*   题目: 20.Valid Parentheses
*   网址:https://oj.leetcode.com/problems/valid-parentheses/
*   结果:AC
*   来源:LeetCode
*   博客:
**********************************/
#include <iostream>
#include <stack>
using namespace std;

class Solution {
public:
    bool isValid(string s) {
        bool result = false;
        int len = s.length();
        if(len <= 1){
            return result;
        }//if
        stack<char> brackets;
        for(int i = 0;i < len;++i){
            // '(' '[' '{' 进栈
            if(s[i] == '(' || s[i] == '[' || s[i] == '{'){
               brackets.push(s[i]);
               continue;
            }//if
            // 否则出栈,比较
            if(!brackets.empty()){
                char bracket = brackets.top();
                brackets.pop();
                if(s[i] == ')' && bracket != '('){
                    return false;
                }//if
                if(s[i] == ']' && bracket != '['){
                    return false;
                }//if
                if(s[i] == '}' && bracket != '{'){
                    return false;
                }//if
            }//if
            // 数目不匹配
            else{
                return false;
            }
        }//for
        if(!brackets.empty()){
            return false;
        }//if
        return true;
    }
};

int main(){
    Solution solution;
    string s = "((";
    bool result = solution.isValid(s);
    // 输出
    cout<<result<<endl;
    return 0;
}

时间: 2024-09-20 06:36:01

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

LeetCode 20 Valid Parentheses(有效的括号)

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

[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

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

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

leetcode 22 Generate Parentheses关于C++ string的问题

问题描述 leetcode 22 Generate Parentheses关于C++ string的问题 /* 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: "((()))", "(()())", "(())

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

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

[LeetCode]125.Valid Palindrome

[题目] Valid Palindrome  Total Accepted: 3479 Total Submissions: 16532My Submissions Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a pa

[LeetCode]36.Valid Sudoku

[题目] Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. [题意] 假设一个数独是有效的,则它符合数独游戏 规则(点击打开