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 class Solution { public int longestValidParentheses(String s) { if (s == null || s.length() == 0) { return 0; } Stack<Integer> stack = new Stack<Integer>(); int start = 0; int res = 0; for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == '(') { stack.push(i); } else { if (stack.isEmpty()) { start = i + 1; } else { stack.pop(); if (stack.isEmpty()) { res = Math.max(res, i-start+1); } else { res = Math.max(res, i-stack.peek()); } } } } return res; } }
相关推荐
java java_leetcode题解之Longest Valid Parentheses.java
js js_leetcode题解之32-longest-valid-parentheses.js
c语言入门 C语言_leetcode题解之32-longest-valid-parentheses.c
java lru leetcode ...Parentheses](./leetcode/动态规划-Longest Valid Parentheses.java) [动态规划-Maximum Length of Repeated Subarray](./leetcode/动态规划-Maximum Length of Repeated Subar
Longest Palindromic Substring 最长回文子串 7 Reverse Integer 整数反转 9 Palindrome Number 回文数 11 Container With Most Water 盛最多水的容器 13 Roman to Integer 罗马数字转整数 14 Longest Common Prefix...
2. **Longest Valid Parentheses**:找到有效括号字符串中的最长连续子串长度,同样利用栈记录括号状态来计算最长长度。 3. **Evaluate Reverse Polish Notation**:逆波兰表达式求值,使用栈处理运算符和操作数,...
在本资源包中,主题聚焦于C语言的基础学习,特别是针对LeetCode编程挑战中的第32题——"最长有效括号"(Longest ValidParentheses)的解法。LeetCode是一个在线平台,提供了各种算法问题,旨在提升程序员的编程技能...
leetcode怎么改密码 Code leetcode easy 测试一下本地的... emmmmm之前修改了一下,现在用ssh提交 应该不用输入密码了吧 ~~emmmmm 先在这里立个flag!!...Longest Valid Parentheses :cross_mark:暴力解法(未通过)
...The number of questions is increasing recently. Here is the classification of all `468` questions. ...I'll keep updating for full summary and better solutions....|-----|---------------- | --------------- |...
在本文中,我们探讨了JavaScript中栈数据结构的使用,并以Leetcode第32题Longest Valid Parentheses(最长有效括号)为例,说明了栈结构在解决特定问题时的应用。下面详细讲解了栈的定义、基本操作以及如何使用栈...
第四章 Leetcode 题解 ...20. Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 23. Merge k Sorted Lists 24. Swap Nodes in Pairs 25. Reverse Nodes in k-Group 26. Remove Dupli
Valid Parentheses 21. Merge Two Sorted Lists 22. Generate Parentheses 25. Reverse Nodes in k-Group 26. Remove Duplicates from Sorted Array 27. Remove Element 28. Implement strStr() 3
leetcode 530 ** LeetCode 题目更新 ** 用来记录业余时间所做的算法题目,保持对于数据结构的熟悉。 ** ...Valid Parentheses 022 Generate Parentheses 028 Implement strStr() 031 Next Permutat
leetcode双人赛LeetCode 编号 题目 难度 题型 备注 Two Sum 简单 Add ...Longest ...Longest ...Longest ...Parentheses 简单 堆叠 重要 Merge Two Sorted Lists 简单 链结串列 重要 Remove Duplicates from
20.Valid 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 ...
Parentheses 用栈判断括号匹配 Regular Expression Matching 递归匹配 wildcard matching 动态规划 longest common prefix , 简单 valid number, hard, 用有限自动机 integer to roman ,easy , 模拟 roman to ...
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
lru cache leetcode #算法解题报告 主要记录我每天做的题目,包括leetcode, 剑指offer等在线编程平台,以前做过的等时间够再一起分享。 使用swift解题 30-Day ...Parentheses 21.Merge Two Sorted L