20. Valid Parentheses(easy)

版权声明:文章都是原创,转载请注明~~~~ https://blog.csdn.net/SourDumplings/article/details/86529346

Easy

2371117FavoriteShare

Given a string containing just the characters '('')''{''}''[' and ']', determine if the input string is valid.

An input string is valid if:

  1. Open brackets must be closed by the same type of brackets.
  2. Open brackets must be closed in the correct order.

Note that an empty string is also considered valid.

Example 1:

Input: "()"

Output: true

Example 2:

Input: "()[]{}"

Output: true

Example 3:

Input: "(]"

Output: false

Example 4:

Input: "([)]"

Output: false

Example 5:

Input: "{[]}"

Output: true

 

C++:

/*
 @Date    : 2019-01-07 20:28:19
 @Author  : 酸饺子 ([email protected])
 @Link    : https://github.com/SourDumplings
 @Version : $Id$
*/

/*
https://leetcode.com/problems/valid-parentheses/
 */

class Solution
{
public:
    bool isValid(string s)
    {
        stack<char> S;
        for (char c : s)
        {
            switch (c)
            {
                case '[':
                case '{':
                case '(':
                S.push(c); break;
                case ']':
                {
                    if (!S.empty() && S.top() == '[')
                    {
                        S.pop();
                        break;
                    }
                    else
                        return false;
                }
                case '}':
                {
                    if (!S.empty() && S.top() == '{')
                    {
                        S.pop();
                        break;
                    }
                    else
                        return false;
                }
                case ')':
                {
                    if (!S.empty() && S.top() == '(')
                    {
                        S.pop();
                        break;
                    }
                    else
                        return false;
                }
            }
        }
        if (S.empty())
        {
            return true;
        }
        else
            return false;
    }
};

Java:

/**
 * @Date    : 2019-01-07 20:35:41
 * @Author  : 酸饺子 ([email protected])
 * @Link    : https://github.com/SourDumplings
 * @Version : $Id$
 *
 * https://leetcode.com/problems/valid-parentheses/
*/

class Solution
{
    public boolean isValid(String s)
    {
        Stack<Character> S = new Stack<Character>();
        for (int i = 0; i != s.length(); ++i)
        {
            Character c = new Character(s.charAt(i));
            if (c == '(' || c == '[' || c == '{')
            {
                S.push(c);
            }
            else if (c == ')')
            {
                if (!S.empty() && S.peek() == '(')
                {
                    S.pop();
                }
                else
                    return false;
            }
            else if (c == ']')
            {
                if (!S.empty() && S.peek() == '[')
                {
                    S.pop();
                }
                else
                    return false;
            }
            else if (c == '}')
            {
                if (!S.empty() && S.peek() == '{')
                {
                    S.pop();
                }
                else
                    return false;
            }
        }
        if (S.empty())
        {
            return true;
        }
        else
            return false;
    }
}

猜你喜欢

转载自blog.csdn.net/SourDumplings/article/details/86529346