【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】

【032-Longest Valid Parentheses(最长有效括号)】


【LeetCode-面试算法经典-Java实现】【所有题目目录索引】

原题

  Given a string containing just the characters ‘(‘ and ‘)‘, find the length of the longest valid (well-formed) parentheses substring.

  For "(()", the longest valid parentheses substring is "()", which has length = 2.

  Another example is ")()())", where the longest valid parentheses substring is "()()", which has length = 4.

题目大意

  给定一个字符串,只包含小括号号,求最长的合法的小括号的数目。

解题思路

  使用栈来实现

代码实现

算法实现类

import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;

public class Solution {

    public int longestValidParentheses(String s) {
        // 用于记录待匹配的左括号和右括号的位置
        Stack<Integer> st = new Stack<>();
        int max = 0;
        for (int i = 0; i < s.length(); i++) {

            // 如是当前字符是右括号,并且记录栈非空,并且前一个字符是左括号
            if (s.charAt(i) == ‘)‘ && !st.isEmpty() && s.charAt(st.peek()) == ‘(‘) {
                // 左括号出栈
                st.pop();
                // 求最大值
                max = Math.max(max, i - ((st.isEmpty()) ? -1 : st.peek()));
            }
            // 其它情况就将字符入栈
            else {
                st.push(i);
            }
        }
        return max;
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47064939

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-14 20:17:30

【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】的相关文章

[LeetCode] 32. Longest Valid Parentheses 最长有效括号

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

[LeetCode] Longest Valid Parentheses 最长有效括号

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

[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: "(()" Output: 2 Explanation: The longest valid parentheses substring is "()" Example

[LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Longest Valid Parentheses (Hard) 链接: 题目:https://oj.leetcode.com/problems/longest-valid-parentheses/ 代码(github):https://github.com/illuz/leetcode 题意: 问一个字

032 Longest Valid Parentheses

题目: 032 Longest Valid Parentheses 这道题是典型的堆栈应用 class Solution: # @param {string} s # @return {integer} def longestValidParentheses(self, s): stack = [] i, last, cur_len, ans = 0, -1, 0, 0 while i < len(s): if s[i] == "(": stack.append(i) else:

Java for LeetCode 032 Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

【leetcode with java】32 Longest Valid Parentheses O(n)

这个题目leetcode上提示用动态规划,但是那样要O(n^2).我自己想出了一个O(n)的算法,并提交通过. [题目] Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring

【LeetCode-面试算法经典-Java实现】【020-Valid Parentheses(括号验证)】

[020-Valid Parentheses(括号验证)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and &qu

【LeetCode-面试算法经典-Java实现】【022-Generate Parentheses(生成括号)】

[022-Generate Parentheses(生成括号)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())&qu

LeetCode 032 Longest Valid Parentheses

题目描述:Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has l