LeetCode 32 最长有效括号
生活随笔
收集整理的這篇文章主要介紹了
LeetCode 32 最长有效括号
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
https://leetcode-cn.com/problems/longest-valid-parentheses/
解決方案
class Solution {public int longestValidParentheses(String s) {int maxans = 0;LinkedList<Integer> stack = new LinkedList<>();stack.push(-1);for (int i = 0; i < s.length(); i++) {if (s.charAt(i) == '(') {stack.push(i);} else {stack.pop();if (stack.isEmpty()) {stack.push(i);} else {maxans = Math.max(i - stack.peek(), maxans);}}}return maxans;} }總結
以上是生活随笔為你收集整理的LeetCode 32 最长有效括号的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode 31 下一个排列
- 下一篇: LeetCode 33 搜索旋转排序数组