4.1.2—栈—Longest Valid Parentheses
来源:互联网 发布:男神的偶像知乎 编辑:程序博客网 时间:2024/06/16 00:31
描述
Given a string containing just the characters ’(’ and ’)’, find the length of the longest valid (wellformed) 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.
Given a string containing just the characters ’(’ and ’)’, find the length of the longest valid (wellformed) 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.
#include <iostream>#include <string>#include<vector>#include <stack>using namespace std;int ValidParenthese(string str){int answer = 0;int last = -1;int length = str.size();stack<int> mystack;for (int i = 0; i<length; i++){if (str[i] == '(')mystack.push(i);else if (str[i] == ')'){if (mystack.empty()){last = i;}//===else{mystack.pop();if (mystack.empty())answer = (i - last)>answer ? (i - last) : answer;elseanswer = (i - mystack.top())>answer ? (i - mystack.top()) : answer;}}}return answer;}int main(){string str = "()()()())))((((()))))";int answer = ValidParenthese(str);cout << answer << endl;}
阅读全文
0 0
- 4.1.2—栈—Longest Valid Parentheses
- 4.1.2 Longest Valid Parentheses
- 4.1.2 Longest Valid Parentheses
- leetcode 032 —— Longest Valid Parentheses
- leetcode题集——longest-valid-parentheses
- Leetcode——32. Longest Valid Parentheses
- <32>——Longest Valid Parentheses
- LeetCode—32 Longest Valid Parentheses
- Leetcode 栈 Longest Valid Parentheses
- 4.1.1—栈—Valid Parentheses
- 【leetcode】String——Longest Valid Parentheses(32)
- leetcode 32. Longest Valid Parentheses——(use stack)
- 每日一道算法题——Longest Valid Parentheses
- leetcode字符串dp——32. Longest Valid Parentheses
- [LeetCode]—Longest Valid Parentheses 最长括号匹配
- 栈——longest-valid-parentheses(最长有效括号长度)
- 【栈、动态规划】Longest Valid Parentheses
- LeetCode 之 Longest Valid Parentheses(栈)
- 4.1.1—栈—Valid Parentheses
- php用curl获取sessionid
- HDU4704Sum 费马小定理+大数取模
- Class……not found (ONETHINK框架)
- PAT 乙级 1019.数字黑洞(20)
- 4.1.2—栈—Longest Valid Parentheses
- android audio volume
- SpringMVC4.3x教程之八异常的三种处理方式
- 4.1.3—栈—Largest Rectangle in Histogram
- TypeError: cannot perform reduce with flexible type
- 5.1 Tensorflow:图与模型的加载与存储
- docker
- select和epoll的区别
- 4.1.4—栈—Evaluate Reverse Polish Notation