zoukankan      html  css  js  c++  java
  • 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
    

    这道题看似简单,但是我还是没有一次AC,思路很简答,就是入栈与出栈,遇到左括号入栈,遇见如果有匹配的括号将其出栈,否则返回false,到最后如果栈空则说明括号是合法且匹配的,类似的题目有Generate ParenthesesLongest Valid Parentheses

     1 class Solution {
     2 public:
     3     bool isValid(string s) {
     4         stack<char> sta;
     5         for (auto c : s)
     6         {
     7             if (c == '}')
     8             {
     9                 if (sta.empty() || sta.top() != '{')
    10                     return false;
    11                 else
    12                     sta.pop();
    13             }
    14             else if (c == ']')
    15             {
    16                 if (sta.empty() || sta.top() != '[')
    17                     return false;
    18                 else
    19                     sta.pop();
    20             }
    21             else if (c == ')')
    22             {
    23                 if (sta.empty() || sta.top() != '(')
    24                     return false;
    25                 else
    26                     sta.pop();
    27             }
    28             else
    29                 sta.push(c);
    30         }
    31         return sta.empty();
    32     }
    33 };

    有几个地方要注意:1、调用栈的top()函数时一定要先判决栈是否为空,否则会报错;2、多个else if 条件时,一定要用合适的方式进行分类讨论,否则容易重复或者遗漏

    以下是一种更简练的写法:

     1 class Solution {
     2 public:
     3     bool isValid(string s) {
     4         stack<char> parentheses;
     5         for (int i = 0; i < s.size(); ++i) {
     6             if (s[i] == '(' || s[i] == '[' || s[i] == '{') parentheses.push(s[i]);
     7             else {
     8                 if (parentheses.empty()) return false;
     9                 if (s[i] == ')' && parentheses.top() != '(') return false;
    10                 if (s[i] == ']' && parentheses.top() != '[') return false;
    11                 if (s[i] == '}' && parentheses.top() != '{') return false;
    12                 parentheses.pop();
    13             }
    14         }
    15         return parentheses.empty();
    16     }
    17 };

    类似的解答请参考:http://www.cnblogs.com/grandyang/p/4424587.html

  • 相关阅读:
    hive 之start hiveServer2 ,thriftServer失败
    sqoop 导入mysql中表存在联合主键
    hive metastore Server 出现异常
    hiveF 函数解析时间问题
    hive 动态分区数设置
    sqoop 操作从hdfs 导入到mysql中语句
    hive 锁表问题
    在hive中直接对timestamp类型取max报错
    Qt程序crash信息的捕捉与跟踪(转)
    功能快捷键如注释、声明和实现之间切换(转)
  • 原文地址:https://www.cnblogs.com/dapeng-bupt/p/9863292.html
Copyright © 2011-2022 走看看