32. 最長有效括號 - 力扣(LeetCode)
代碼區:
#include<stack>
#include<string>
/*最長有效*/
class Solution {
public:int longestValidParentheses(string s) {stack<int> st;int ans=0;int n=s.length();st.push(-1);for(int i=0;i<n;i++){if(s[i]=='('){st.push(i);}else {st.pop();if(st.empty()){st.push(i);}else{ans=max(ans,i-st.top());}}}return ans;}
};
歡迎各位讀者提出意見。
(菜菜奮斗小日記)