http://oj.leetcode.com/problems/longest-valid-parentheses/

最大括号匹配长度,括号是可以嵌套的

#include <string>
#include <stack>
#include <vector>
#include <iostream>
using namespace std;
class Solution {
public:
int longestValidParentheses(string s) {
const int s_len = s.size(); stack<int> indexstack;
vector<bool> flagvector;
int templen = ;
int maxlen = ;
flagvector.resize(s_len); for(int index = ;index<s_len;index++)
flagvector[index] = false; for(int i = ;i<s_len;i++)
{
if(s[i]==')')
{
if(indexstack.size()!=)
{
flagvector[indexstack.top()] = true;
flagvector[i] = true;
indexstack.pop();
} }
if(s[i]=='(')
{
indexstack.push(i);
}
}
for(int index = ;index<s_len;index++)
{
if(flagvector[index]==false)
{
maxlen = maxlen>templen?maxlen:templen;
templen = ;
}
else
templen++;
}
maxlen = maxlen>templen?maxlen:templen;
return maxlen;
}
};
#include <iostream>
#include "cla.h"
using namespace std;
int main()
{
Solution * mysolution = new Solution();
string str = "";
cout<<mysolution->longestValidParentheses(str)<<endl;
return ;
}

LeetCode OJ——Longest Valid Parentheses的更多相关文章

  1. [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

    指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...

  2. Java for LeetCode 032 Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. LeetCode 之 Longest Valid Parentheses(栈)

    [问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest v ...

  4. [LeetCode] 32. Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  5. leetcode 32. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  6. 【leetcode】Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  7. 【leetcode】 Longest Valid Parentheses (hard)★

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  8. Java [leetcode 32]Longest Valid Parentheses

    题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...

  9. [leetcode]32. Longest Valid Parentheses最长合法括号子串

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

随机推荐

  1. 【差分约束】poj1275Cashier Employment

    比较经典的差分约束 Description A supermarket in Tehran is open 24 hours a day every day and needs a number of ...

  2. k8s的资源限制及资源请求

    容器的资源需求及限制:  需求:requests   ##定义容器运行时至少需要资源  限制:limits     ##定义容器运行时最多能分配的资源    requests:pod.spec.con ...

  3. MySQL创建数据库,用户,赋予权限

    CREATE DATABASE 'voyager'; CREATE DATABASE `voyager`; CREATE USER 'dog'@'localhost' IDENTIFIED BY '1 ...

  4. 使用el-checkbox实现全选,点击失效没有反应

    最近在公司接收到了一个需求,给收藏夹的书籍添加批量.全选删除实现思路:点击全选改变item的checked,改变item的checked,重新便利一下所有item的checked来改变全选的selec ...

  5. ultraedit编辑器破解版下载

    ultraedit一款功能丰富的网站建设软件,需要的朋友可以看看. 百度百科:UltraEdit 是一套功能强大的文本编辑器,可以编辑文本.十六进制.ASCII 码,完全可以取代记事本(如果电脑配置足 ...

  6. COMP9021--6.6

    1. 在print结尾处添加end='' print默认在字符串结尾处添加换行符,添加end=''后表示这个语句并没有结束,结尾不换行 2. 为了减少重复代码以及便于修改,我们可以编写函数 1) 函数 ...

  7. python中文件操作的六种模式及对文件某一行进行修改的方法

    一.python中文件操作的六种模式分为:r,w,a,r+,w+,a+ r叫做只读模式,只可以读取,不可以写入 w叫做写入模式,只可以写入,不可以读取 a叫做追加写入模式,只可以在末尾追加内容,不可以 ...

  8. Find a way HDU - 2612(bfs)

    Find a way Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  9. JDK1.8 HashMap$TreeNode.rotateLeft 红黑树左旋

    红黑树介绍 1.节点是红色或黑色. 2.根节点是黑色. 3.每个叶子节点都是黑色的空节点(NIL节点). 4 每个红色节点的两个子节点都是黑色.(从每个叶子到根的所有路径上不能有两个连续的红色节点) ...

  10. redis--py操作redis【转】

    Python操作redis 请给作者点赞--> 原文链接 python连接方式:点击 下面介绍详细使用 1.String 操作 redis中的String在在内存中按照一个name对应一个val ...