leetcode第20题--Valid Parentheses

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

判断输入的括号是不是合法。这题在大二的时候就遇到过了。想不到我的记性这么好。因为当时我什么都不懂,刚学数据结构,然后有个ACMer是助教,他给我讲解了这题。然后我就一直都记得了。也不知道那个师兄现在在哪里牛逼。

回到题目,就是用栈实现。

class Solution {
    private:
    char anti(char c)
    {
        if (c == ‘]‘)
            return ‘[‘;
        if (c == ‘)‘)
            return ‘(‘;
        if (c == ‘}‘)
            return ‘{‘;
    }
public:
    bool isValid(string s) {
        int len = s.size();
        if (len%2)
        { return false;}
        if(s.size() == 0 || s[0] == ‘)‘ || s[0] == ‘]‘ || s[0] == ‘}‘)
            return false;
        stack<char> st;
        for (int i = 0; i < len; i++)
        {
            if (s[i] == ‘(‘ || s[i] == ‘{‘ || s[i] == ‘[‘)
                st.push(s[i]);
            else
                if (st.top() == anti(s[i]))
                    st.pop();
                else
                    return false;
        }
        if (st.empty())
            return true;
        else
            return false;
    }
};

代码写得有点挫,大概就是这个思路,然后Accept了

时间: 2024-12-07 01:02:20

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 input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]&q

LeetCode 第 367 题 (Valid Perfect Square)

LeetCode 第 367 题 (Valid Perfect Square) Given a positive integer num, write a function which returns True if num is a perfect square else False. Note: Do not use any built-in library function such as sqrt. Example 1: Input: 16 Returns: True Example 2

LeetCode解题报告—— Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. Example 1: Input: "(()" Output: 2 Explanation: The longest valid parentheses substring is "()" Example

[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

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

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第35题--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. Note:A valid Sudoku board (partial

【leetcode】32. Longest Valid Parentheses

这题不能使用递归来写,因为有一个输入长度是17173,会爆栈.因此得手动模拟栈调用. public class Solution { private static class StackFrame { public final char left; private int num; public StackFrame(char left) { this.left = left; } @Override public String toString() { return "StackFrame [