leetcode-20:Valid Parentheses有效的括号(括号匹配)

题目:

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

给定一个只包括 '('')''{''}''['']' 的字符串,判断字符串是否有效。

有效字符串需满足:

  1. 左括号必须用相同类型的右括号闭合。
  2. 左括号必须以正确的顺序闭合。

注意空字符串可被认为是有效字符串。

示例 1:

输入: "()"
输出: true

示例 2:

输入: "()[]{}"
输出: true

示例 3:

输入: "(]"
输出: false

示例 4:

输入: "([)]"
输出: false

示例 5:

输入: "{[]}"
输出: true

 思路:借用栈。若为({[则入栈,若当前元素为})],看当前栈是否为空并与对应括号匹配, 不匹配返回false,匹配就出栈。知道最终栈为空。

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

猜你喜欢

转载自blog.csdn.net/qq_21997625/article/details/85114121