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

  • 相关阅读:
    Flink基础之实现WordCount程序(Java版本多种写法)
    Redis 在Springboot 中反序列化 LocalDateTime 时报错
    JVM(五)- 堆
    JVM(四)- 虚拟机栈
    JVM(三)- 程序计数器(PC 寄存器)
    JVM(二)- 运行时数据区概述及线程
    JVM(一)
    phpstudy后门漏洞复现
    primefaces 5.x 表达式注入
    ueditor(v1.4.3)文件上传getshell实战复现
  • 原文地址:https://www.cnblogs.com/dapeng-bupt/p/9863292.html
Copyright © 2011-2022 走看看