LeetCode OJ——Longest Valid Parentheses
2013-10-26 10:49
459 查看
http://oj.leetcode.com/problems/longest-valid-parentheses/
最大括号匹配长度,括号是可以嵌套的。
View Code
最大括号匹配长度,括号是可以嵌套的。
#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 = 0; int maxlen = 0; flagvector.resize(s_len); for(int index = 0;index<s_len;index++) flagvector[index] = false; for(int i = 0;i<s_len;i++) { if(s[i]==')') { if(indexstack.size()!=0) { flagvector[indexstack.top()] = true; flagvector[i] = true; indexstack.pop(); } } if(s[i]=='(') { indexstack.push(i); } } for(int index = 0;index<s_len;index++) { if(flagvector[index]==false) { maxlen = maxlen>templen?maxlen:templen; templen = 0; } else templen++; } maxlen = maxlen>templen?maxlen:templen; return maxlen; } };
View Code
#include <iostream> #include "cla.h" using namespace std; int main() { Solution * mysolution = new Solution(); string str = ""; cout<<mysolution->longestValidParentheses(str)<<endl; return 0; }
相关文章推荐
- 硬盘分区4-如何划分4个以上的分区
- 线性筛数法
- oracle小结
- linux 系统使用/管理之-----Linux常用命令详解
- 给eclipse添加字体,适合给新手看。
- java中对文件解压和压缩的实现
- web.xml配置taglib标签出错
- 张国祥老师为上海景鸿集团讲授流程管理取得圆满成功
- 防止SQL注入的方法
- List<> ArrayList
- JDK1.6新特性,网络增强(Networking features and enhancements)
- spring的jar包下载地址
- Objective-C入门基础
- ubuntu 安装 jdk7 + eclipse (我初学时候的方法,现在我不建议这么安装)
- 广州最好的网页平面UI设计培训 UI设计师的摇篮
- java提高篇(九)-----实现多重继承
- ubuntu13.10 安装 jdk7 + eclipse 【robby_chan】
- IOS7开发~解决UIImagePickerController出现时候显示StatusBar
- 多线程的简单应用
- 10 个你值得拥有的 Vim 配色方案