码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode – Refresh – Longest Valid Parentheses

时间:2015-03-20 09:12:56      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

Notes:

Do not forget to clean the total and rec.

 1 class Solution {
 2 public:
 3     int longestValidParentheses(string s) {
 4         int len = s.size(), total = 0, rec = 0, result = 0;
 5         for (int i = 0; i < len; i++) {
 6             if (s[i] == () {
 7                 total++;
 8                 rec++;
 9             } else {
10                 total++;
11                 rec--;
12             }
13             if (rec < 0) {
14                 total = 0;
15                 rec = 0;
16             } else if (rec == 0) {
17                 result = max(result, total);
18             }
19         }
20         total = 0, rec = 0;
21         for (int i = len-1; i >= 0; i--) {
22             if (s[i] == )) {
23                 total++;
24                 rec++;
25             } else {
26                 total++;
27                 rec--;
28             }
29             if (rec < 0) {
30                 total = 0;
31                 rec = 0;
32             } else if (rec == 0) {
33                 result = max(result, total);
34             }
35         }
36         return result;
37     }
38 };

 

LeetCode – Refresh – Longest Valid Parentheses

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/4352706.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!