Leetcode32 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 length = 2.

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

Solution1

  • 这种题其实没有什么诀窍,多举几个实例渐渐归纳出一些规律出来。这里用栈来实现:
import java.util.Stack;
public class Solution {
    public int longestValidParentheses(String s) {
        Stack<Integer> stack = new Stack<Integer>();
        int result = 0;
        for(int i=0,start=0;i<s.length();i++){
            char c = s.charAt(i);
            if(c=='(') stack.push(i);//将左括号的位置都记录下来
            else{
                if(stack.empty()) start = i+1;//更新有可能成为最左边边界的位置
                else{
                    stack.pop();
                    if(stack.empty()) result = Math.max(result,i-start+1);//说明已经和最左边边界连通起来了
                    else result = Math.max(result,i-stack.peek());//未和最左边边界连通,但是可以和之前的某个左括号组成有效的括号对
                }
            }
        }
        return result;        
    }
}

Solution2

  • 解法2和解法1思路是一致的,只不过用了更加巧妙的办法使得程序更简短,但是理解起来其实更加复杂了。
import java.util.Stack;
public class Solution {
    public int longestValidParentheses(String s) {
        Stack<Integer> stack = new Stack<Integer>();
        int result = 0;
        for(int i=0,start=0;i<s.length();i++){
            if(s.charAt(i)==')'&&!stack.empty()&&s.charAt(stack.peek())=='('){
                stack.pop();
                result = Math.max(result,i-(stack.empty()?-1:stack.peek()));
            }else stack.push(i);//这里相当于将解法一的start位置也存入了stack
        }
        return result;    
    }
}

Solution3

  • 动态规划的解法。
import java.util.Stack;
public class Solution {
    public int longestValidParentheses(String s) {
        int n = s.length();
        int[] dp = new int[n];
        int left = 0, result = 0;
        for(int i=0;i<n;i++){
            if(s.charAt(i)=='(') left++;//记录当前还有多少左括号未配对
            else if(left>0){//若当前有左括号可与当前的右括号配对
                dp[i] = 2 + dp[i-1];//先将这个配对的左括号计入
                if(i-dp[i]>=0) dp[i] += dp[i-dp[i]];//判断是否和之前的左括号连通了
                left--;//未配对左括号数减一
            }
            result = Math.max(result,dp[i]);
        }
        return result;   
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
LeetCode是一个非常受欢迎的在线编程平台,提供了大量的算法题目,涵盖了各种难度级别和题型。下面是一些常用的LeetCode算法: 1. 两数之和(Two Sum):给定一个整数数组和一个目标值,找出数组中和为目标值的两个数的索引。 2. 反转字符串(Reverse String):反转给定字符串中的字符顺序。 3. 最长公共前缀(Longest Common Prefix):找出一组字符串中最长的公共前缀。 4. 合并两个有序链表(Merge Two Sorted Lists):将两个有序链表合并为一个新的有序链表。 5. 有效的括号(Valid Parentheses):判断给定的字符串中的括号是否有效。 6. 盛最多水的容器(Container With Most Water):给定一组非负整数,表示一组垂直线在x轴上的坐标,找出两条线与x轴组成的容器可以容纳的最大水量。 7. 三数之和(3Sum):给定一个包含n个整数的数组nums,判断nums中是否存在三个元素a,b,c,使得a + b + c = 0。 8. 最长回文子串(Longest Palindromic Substring):找出给定字符串中的最长回文子串。 9. 二叉树的最大深度(Maximum Depth of Binary Tree):计算二叉树的最大深度,即从根节点到最远叶子节点的最长路径上的节点数。 10. 两个排序数组的中位数(Median of Two Sorted Arrays):给定两个大小为m和n的有序数组,找出这两个数组合并后的中位数。 以上只是LeetCode中的一小部分常用算法题目,LeetCode还有很多其他类型的题目,包括动态规划、回溯、贪心算法等等。如果你有具体的算法问题或者需要更多的题目推荐,请告诉我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值