Skip to content

Commit d8fb0eb

Browse files
Refine
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 7054a89 commit d8fb0eb

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

032_longest_valid_parentheses/valid_parentheses.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
#include <stdlib.h>
33

44

5+
static inline int max(int a, int b)
6+
{
7+
return a > b ? a : b;
8+
}
9+
510
static int longestValidParentheses(char* s)
611
{
712
int i, cap = 18000, invalid = -1;
@@ -16,17 +21,14 @@ static int longestValidParentheses(char* s)
1621
} else {
1722
if (top > stack) {
1823
if (--top == stack) {
19-
/* distancd of the latest ')' */
20-
len = i - invalid;
24+
/* locate the remote ')' */
25+
max_len = max(i - invalid, max_len);
2126
} else {
22-
/* distance of the remote '(' */
23-
len = i - *(top - 1);
24-
}
25-
if (len > max_len) {
26-
max_len = len;
27+
/* locate the remote '(' */
28+
max_len = max(i - *(top - 1), max_len);
2729
}
2830
} else {
29-
/* record the index of last ')' but no push */
31+
/* record the index of the remote ')' and no push */
3032
invalid = i;
3133
}
3234
}

0 commit comments

Comments
 (0)