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. Jquery之 Ajax /json

    前言: Ajax = Asynchronous JavaScript and XML(异步的JavaScript和XML) Ajax不是新的编程语言,而是一种使用现有标准的新方法. Ajax最大的优点 ...

  2. yagmail 邮箱的使用

    文章来源:GITHub:https://github.com/kootenpv/yagmail 安装 pip3 install yagmail pip3 install keyring 简单例子 im ...

  3. GoF23种设计模式之行为型模式之备忘录模式

    一.概述         在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象的外部保存这个状态.以便以后可以将该对象恢复到原先保存的状态. 二.适用性 1.当需要保存一个对象在某个时刻的状态( ...

  4. init_bootmem_node

    初始化pg_data_t->bdtat结构体, /* * node_bootmem_map is a map pointer - the bits represent all physical ...

  5. CodeForces - 485D Maximum Value (数学)

    题意: n个数,求出这些数中满足 ai >= aj 的 ai % aj 的最大值. 排序去重,然后对于每一个a[i], 如果找到a[i] 的一个倍数 k*a[i] (k > 1)的位置,那 ...

  6. hdu 5984

    PockyTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submissio ...

  7. poj 2718 切数问题 穷竭搜索

    题意: 给一个已经排序号的数字,从中间切一刀,成两个数,要求这两个数的差最小 思路:暴力比较差最小值 stl中的next_permutation()函数进行排列  注意:这个函数必须从小到大才可以排序 ...

  8. matlab画图颜色设置

    各种颜色属性选项选项意义选项意义'r' 红色 'm' 粉红'g' 绿色 'c' 青色'b' 兰色 'w' 白色'y' 黄色 'k' 黑色各种线型属性选项选项意义选项意义'-' 实线 '--' 虚线': ...

  9. CSS 预处理器框架

    CSS 预处理器框架 可以按照需求来使用别人的代码 1.sass (compass) 2.less (lesshat/EST) 3.提供现成的 mixin 4.类似 JS 类库 ,封装常用功能 css ...

  10. php 图表的操作

    <?php $dir=dirname(__FILE__);//查找当前脚本所在路径 require $dir."/db.php";//引入mysql操作类文件 require ...