`

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 ")()())", where the longest valid parentheses substring is "()()", which has length = 4.

 

public int longestValidParentheses(String s) {
    int n = s.length();
    Stack<Integer> stack = new Stack<>();
    int len = 0;
    for(int i=0; i<n; i++) {
        char c = s.charAt(i);
        if(c == ')' && !stack.isEmpty() && s.charAt(stack.peek()) == '(') {
            stack.pop();
            if(stack.isEmpty()) {
                len = i+1;
            } else {
                len = Math.max(len, i-stack.peek());
            }
        } else {
            stack.push(i);
        }
    }
    return len;
}

 

int longestValidParentheses(string s) {
    int res = 0;
    stack<int> st;
    for(int i=0; i<s.size(); i++) {
        if(!st.empty() && s[i]-s[st.top()] == 1) {
            st.pop();
            int start = st.empty() ? -1 : st.top();
            res = max(res, i-start);
        } else {
            st.push(i);
        }
    }
    return res;
}

 

 

分享到:
评论

相关推荐

    C语言-leetcode题解之32-longest-valid-parentheses.c

    其中,编号为32的题目"longest-valid-parentheses"是一个关于字符串处理的经典问题,要求编写代码找出给定的括号字符串中最长的有效括号子串长度。 在解决"longest-valid-parentheses"问题时,需要理解有效括号子串...

    java-leetcode题解之Longest Valid Parentheses.java

    其中,“Longest Valid Parentheses”是一道在LeetCode上常见的算法题目,要求编写一个函数来找出输入字符串中有效的括号配对的最大长度。 在Java中,解决这类问题通常需要对字符串进行遍历,并且利用栈、动态规划...

    js-leetcode题解之32-longest-valid-parentheses.js

    本文是一篇专注于解决LeetCode平台中JavaScript语言编写的“32-最长有效括号”问题的题解文章。在介绍解题思路和实现方法之前,先对这个问题进行详细阐述。问题要求编写一个函数,该函数能够找到给定字符串中合法的...

    _leetcode-python.pdf

    - Valid Parentheses: 验证给定的字符串中的括号是否有效。这通常可以通过栈结构来实现。 - Merge Two Sorted Lists: 合并两个有序链表。 - Generate Parentheses: 生成所有可能的有效的括号组合。 - Merge k Sorted...

    颜色分类leetcode-leetcode-[removed]我对Leetcode问题的解决方案

    颜色分类leetcode My Leetcode Problems Solutions Using javascript(ES6) 1 Two Sum 两数之和 5 Longest Palindromic Substring 最长回文子串 7 Reverse Integer 整数反转 9 Palindrome Number 回文数 11 Container...

    gasstationleetcode-leetcode-rust:莱特代码休息

    加油站 leetcode 力码锈 问题 # 标题 命令 1 cargo run ...3-longest-substring-without-repeating-...20-valid-parentheses 21 cargo run --bin 21-merge-two-sorted-lists 27 cargo run --bin 27-remove-element 28

    leetcode卡-Array-LeetCode-Solution:数组-LeetCode-解决方案

    比如"有效的括号"(Valid Parentheses)问题,可以使用栈来检查括号配对的有效性。 3. **双指针法**:在数组中使用两个指针同时移动,常用于查找中间元素、解决区间问题等。如"寻找旋转排序数组中的最小值"(Find ...

    javalruleetcode-leetcode-java:力码笔记

    Parentheses 26.Remove Duplicates from Sorted Array 53.Maximum Subarray 70.Climbing Stairs 121.Best Time to Buy and Sell Stock 122.Best Time to Buy and Sell Stock II 123.Best Time to Buy and Sell Stock...

    javalruleetcode-learn-algorithms::laptop:Java实现的各种算法题解

    java lru leetcode ...Parentheses](./leetcode/动态规划-Longest Valid Parentheses.java) [动态规划-Maximum Length of Repeated Subarray](./leetcode/动态规划-Maximum Length of Repeated Subar

    leetcode卡-LeetCode-HashTable:此存储库包含HashTable探索卡中所有问题的解决方案

    3. **集合操作**:如“有效的括号”(Valid Parentheses),哈希表可以用于存储未闭合的括号,以便检查括号匹配性。 4. **最近点对**:在“最接近的三数之和”(3Sum Closest)问题中,双指针配合哈希表可以快速...

    Leetcode-best-DSA-问题:必须执行这些leetcode编码问题以提高您的问题解决能力

    比如“有效的括号”(Valid Parentheses)使用栈来检查括号的正确性,而“用队列实现栈”(Implement Queue using Stacks)则展示了它们的灵活应用。 3. **堆**:最大堆和最小堆常用于优先队列和优化问题。例如,...

    javalruleetcode-LeetCode::lollipop:个人LeetCode习题解答仓库-多语言

    java lru leetcode :ice_cream: LeetCode ...Parentheses 26 Remove Duplicates from Sorted Array 48 Rotate Image 53 Maximum Subarray 55 Jump Game 56 Merge Intervals 64 Minimum Path Sum 73

    丢失的最小正整数leetcode-LeetCodePractice:我的LeetCode练习

    Longest Common Prefix:这道题和大家思路一样,都是取第一个字符串里的字节与后面的进行比较,只是要注意,针对空vector的返回,针对vector只有一个值时候的返回,和一些通过设置更好初始值进行优化。 20. Valid ...

    Leetcode-note:刷题笔记

    "有效的括号"(Valid Parentheses)则涉及到了栈的应用。 五、持续学习与提升 1. 学习社区:参与LeetCode社区讨论,查看他人的解题思路,可以拓宽视野,提高问题解决能力。 2. 定期复习:定期回顾做过的题目,加深...

    LeetCode-Java

    二叉树问题如"Binary Tree Inorder Traversal",链表问题如"Reverse Linked List",栈的应用如"Valid Parentheses",哈希表的使用则常见于查找和去重问题。 3. **排序和搜索**:快速排序、归并排序、二分查找等是...

    leetcode2-Algorithms-Practice:创建此repo是为了跟踪我在解决问题方面的进展

    Longest Common Prefix 运行时间:40 毫秒内存使用:13.9 MB 20. Valid Parentheses 运行时间:40 毫秒内存使用:13.8 MB 22. Generate Parentheses 运行时间:164 毫秒内存使用:22.5 MB 26. Remove Duplicates ...

    LeetCode最全代码

    # [LeetCode](https://leetcode.com/problemset/algorithms/) ![Language](https://img.shields.io/badge/language-Python%20%2F%20C++%2011-orange.svg) [![License]...

    leetcode中国-leetcode:leetcode刷题

    Parentheses 用栈判断括号匹配 Regular Expression Matching 递归匹配 wildcard matching 动态规划 longest common prefix , 简单 valid number, hard, 用有限自动机 integer to roman ,easy , 模拟 roman to ...

    leetcode530-algorithm:算法

    leetcode 530 ** LeetCode 题目更新 ** 用来记录业余时间所做的算法题目,保持对于数据结构的熟悉。 ** ...Valid Parentheses 022 Generate Parentheses 028 Implement strStr() 031 Next Permutat

Global site tag (gtag.js) - Google Analytics