Valid Parentheses -- leetcode

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, vector<char> > lifo;
        lifo.push('#');

        for (size_t i=0; i<s.size(); i++) {
                if (s[i] == '{' ||
                    s[i] == '[' ||
                    s[i] == '(')
                        lifo.push(s[i]);
                else {
                        const char ch = lifo.top();
                        lifo.pop();
                        if ((s[i] == '}' && ch != '{') ||
                            (s[i] == ']' && ch != '[') ||
                            (s[i] == ')' && ch != '('))
                                return false;
                }
        }

        lifo.pop();
        return lifo.empty();
    }
};

提前进栈一个字符,省得循环中检查栈空了。

在leetcode上的运行时间为4ms。

时间: 2024-10-28 10:19:43

Valid Parentheses -- leetcode的相关文章

Longest Valid Parentheses leetcode java

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

Valid Parentheses leetcode java

题目: 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 "([)]&

Valid Parentheses [LeetCode 20]

1- 问题描述 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 "(

Longest Valid Parentheses Leetcode 32 一种奇特的解法

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

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 @Leetcode -- Python

http://oj.leetcode.com/problems/longest-valid-parentheses/ 1 class Solution: 2 # @param s, a string 3 # @return an integer 4 def longestValidParentheses(self, s): 5 if s == '' or s == '(' or s == ')': 6 return 0 7 stack = [(-1, ')')] 8 maxLen = 0 9 f

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: Valid Parentheses [020]

[题目] 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][Python]32: Longest Valid Parentheses

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 32: Longest Valid Parentheseshttps://oj.leetcode.com/problems/longest-valid-parentheses/ Given a string containing just the characters '(' and ')',find the length of the longest valid (well-fo