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.
維持一個字元棧和位置棧,字元棧記錄沒有匹配的字元,位置棧記錄每一個入棧的字元的位置,如果入棧的是右括號,並且棧頂為左括號,則退棧,求出當前連續退棧的字元長度,求出這個最大值就是。
左括號入棧和沒有匹配的右括號入棧
int longestValidParentheses(char* s) { int tmp=0,i=0, max=0; int s_length=strlen(s); char* s_stack=malloc(sizeof(char)*s_length); int* number_stack=malloc(sizeof(int)*s_length); int s_stack_index=0; while(i<s_length){ if(s[i]=='('){ s_stack[s_stack_index]=s[i]; number_stack[s_stack_index]=i; s_stack_index++; } else { if (s_stack_index>=1 && s_stack[s_stack_index-1]=='('){ s_stack_index--; if(s_stack_index==0) tmp=i+1; // 棧裡的出棧出完了的話,說明出棧完 else tmp=i-number_stack[s_stack_index-1]; if(tmp>max) max=tmp; } else { s_stack[s_stack_index]=s[i]; number_stack[s_stack_index]=i; s_stack_index++; } } i++; } free(s_stack); free(number_stack); return max; }
相關推薦
Longest Valid Parentheses 最長有效括號對
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For
LeetCode 32. Longest Valid Parentheses(最長有效括號)
原題 Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (well-formed) parentheses substr
[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 long
LeetCode 32 — Longest Valid Parentheses(最長有效括號)
Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (well-formed) parentheses substring
leetcode 32. Longest Valid Parentheses 最長有效括號長度
Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (well-formed) parentheses substring
longest valid parentheness 最長有效括號問題
題目要求: 輸入一個字串s,裡面在只包含兩種字元 '('和 ')',例如s=“(()))(()” 找到最長的一個子串,且其包含的括號成對合法出現,例如s中的第2到3個字元子串“()”,但它不是最長的,最長的是第1到4個字元子串“(())”。
LeetCode:32. Longest Valid Parentheses(最長的有效匹配串)
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
[Swift]LeetCode32. 最長有效括號 | Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. Exampl
LeetCode 32 Longest Valid Parentheses(最長有效括號)(*)
翻譯 給定一個僅僅包含“(”或“)”的字串,找到其中最長有效括號子集的長度。 對於“(()”,它的最長有效括號子集是“()”,長度為2。 另一個例子“)()())”,它的最長有效括號子集是“()()”,其長度是4。 原文 Given a strin
leetcode第32題:最長有效括號(遇到一個奇葩的錯誤)
問題描述: 給一個只包含 '(' 和 ')' 的字串,找出最長的有效(正確關閉)括號子串的長度。 對於 "(()",最長有效括號子串為 "()" ,它的長度是 2。 另一個例子 ")()())",最長有效括號子
Leetcode演算法——32、最長有效括號字串
給定一個字串,只包含’(‘和’)’。 要求找到最長的有效的子串。 Example 1: Input: “(()” Output: 2 Explanation: The longest valid parentheses substring is “()” Example 2:
LeetCode---32.最長有效括號
題目來源:https://leetcode-cn.com/problems/longest-valid-parentheses/description/ 題目描述: 演算法描述: 1.定義一個maxLength記錄最長有效括號長度,beginIndex記錄有效括號的起始位置。
leetcode 32 最長有效括號 O(N)時間解法
題目: 給一個只包含 ‘(’ 和 ‘)’ 的字串,找出最長的有效(正確關閉)括號子串的長度。 對於 “(()”,最長有效括號子串為 “()” ,它的長度是 2。 另一個例子 “)()())”,最長有效括號子串為 “()()”,它的長度是 4。 連結: https://le
Leetcode 32:最長有效括號(最詳細的解法!!!)
給定一個只包含 '(' 和 ')' 的字串,找出最長的包含有效括號的子串的長度。 示例 1: 輸入: "(()" 輸出: 2 解釋: 最長有效括號子串為 "()" 示例 2: 輸入: ")()())" 輸出: 4 解釋: 最長有效括號子串為 "()()" 解題思路
leetcode題庫——最長有效括號
題目描述: 給定一個只包含 '(' 和 ')' 的字串,找出最長的包含有效括號的子串的長度。 示例 1: 輸入: "(()" 輸出: 2 解釋: 最長有效括號子串為 "()" 示例 2: 輸入: ")()())" 輸出: 4
Leetcode演算法Java全解答--32. 最長有效括號
Leetcode演算法Java全解答–32. 最長有效括號 文章目錄 Leetcode演算法Java全解答--32. 最長有效括號 題目 想法 結果 總結 程式碼 我的答案 大佬們的答案
棧-LeetCode32-最長有效括號
題目 給定一個只包含 '(' 和 ')' 的字串,找出最長的包含有效括號的子串的長度。 示例 1: 輸入: "(()" 輸出: 2 解釋: 最長有效括號子串為 "()" 示例 2: 輸入: ")()())" 輸出: 4 解釋: 最長有效括號子串為 "()()" 思路 明顯地,使用棧
LeetCode32-最長有效括號
最近這兩天有斷更了,大家知道是為什麼嗎?肯定不是因為是我懶哈(大家想要笑也要憋著)這兩天一直在研究回溯法,被折磨的死去活來的。終於是有些收穫了,所以會在這兩天再把我的一些關於解回溯法題目的心得分享出來,希望大家會有些收穫。寫的都會是很通俗易懂的大白話,會教大家怎麼寫遞迴的過程,因為回溯法雖然好用,但
【LeetCode】32. 最長有效括號
題目描述 給定一個只包含 '(' 和 ')' 的字串,找出最長的包含有效括號的子串的長度。 示例 1: 輸入: "(()" 輸出: 2 解釋: 最長有效括號子串為 "()" 示例 2: 輸入: ")()())" 輸出: 4 解釋: 最長有效括號子串為 "()()"
32. 最長有效括號
給定一個只包含 ‘(’ 和 ‘)’ 的字串,找出最長的包含有效括號的子串的長度。 示例 1: 輸入: “(()” 輸出: 2 解釋: 最長有效括號子串為 “()” 示例 2: 輸入: “)()())”