zoukankan      html  css  js  c++  java
  • [LeetCode] 32. 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: s = "(()"
    Output: 2
    Explanation: The longest valid parentheses substring is "()".
    

    Example 2:

    Input: s = ")()())"
    Output: 4
    Explanation: The longest valid parentheses substring is "()()".
    

    Example 3:

    Input: s = ""
    Output: 0

    Constraints:

    • 0 <= s.length <= 3 * 104
    • s[i] is '(', or ')'.

    最长有效括号。

    给你一个只包含 '(' 和 ')' 的字符串,找出最长有效(格式正确且连续)括号子串的长度。

    这个题可以用动态规划做,但是不是很容易理解,我这里给出一个栈的解法。首先创建一个 stack,然后创建一个 start 变量,初始化 -1,因为 start 是记录最后一个无效的 index。接着开始遍历input

    • 遇到左括号,无条件放入stack
    • 遇到右括号,不需要将右括号入栈,但是需要做一些判断
      • 判断此时栈是否为空
        • 如果为空,说明之前的括号对都处理完毕了,则将start = i,说明之后的有效括号对(如果还有的话)的start index是从 i 开始的
        • 如果不为空,则将栈顶元素pop出来(应该会是个左括号)
          • 如果 pop 之后 stack 为空,则可以结算当前最大长度是res和 i - start 之间的较大者
          • 如果 pop 之后 stack 不为空,还是结算一下此时的最大长度,是 res 和 i - stack.peek() 之间的较大者

    时间O(n)

    空间O(n)

    Java实现

     1 class Solution {
     2     public int longestValidParentheses(String s) {
     3         // corner case
     4         if (s == null || s.length() == 0) {
     5             return 0;
     6         }
     7 
     8         // normal case
     9         Stack<Integer> stack = new Stack<>();
    10         int res = 0;
    11         int start = -1;
    12         for (int i = 0; i < s.length(); i++) {
    13             if (s.charAt(i) == '(') {
    14                 stack.push(i);
    15             } else {
    16                 if (stack.isEmpty()) {
    17                     start = i;
    18                 } else {
    19                     stack.pop();
    20                     if (stack.isEmpty()) {
    21                         res = Math.max(res, i - start);
    22                     } else {
    23                         res = Math.max(res, i - stack.peek());
    24                     }
    25                 }
    26             }
    27         }
    28         return res;
    29     }
    30 }

    JavaScript实现

     1 /**
     2  * @param {string} s
     3  * @return {number}
     4  */
     5 var longestValidParentheses = function(s) {
     6     // corner case
     7     if (s === null || s.length === 0) {
     8         return 0;
     9     }
    10 
    11     // normal case
    12     let start = -1;
    13     let res = 0;
    14     let stack = [];
    15     for (let i = 0; i < s.length; i++) {
    16         if (s.charAt(i) == '(') {
    17             stack.push(i);
    18         } else {
    19             if (stack.length === 0) {
    20                 start = i;
    21             } else {
    22                 stack.pop();
    23                 if (stack.length === 0) {
    24                     res = Math.max(res, i - start);
    25                 } else {
    26                     res = Math.max(res, i - stack[stack.length - 1]);
    27                 }
    28             }
    29         }
    30     }
    31     return res;
    32 };

    LeetCode 题目总结

  • 相关阅读:
    String、StringBuffer、StringBuilder源码解读
    查询条件左边写入函数,导致无法命中索引
    【图形学手记】law of the unconscious statistician
    【图形学手记】蒙特卡洛方法相关笔记
    【图形学手记】抽样分布相关的数学笔记
    C++ lower_bound
    【图形学手记】Inverse Transform Sampling 逆转换抽样
    【Java学习笔记】LinkedList JDK1.6
    【疑难杂症】new Date() 造成的线程阻塞问题
    【疑难杂症】【Solved】maven-compiler-plugin 在 idea 下的问题
  • 原文地址:https://www.cnblogs.com/cnoodle/p/13233582.html
Copyright © 2011-2022 走看看