Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6123720

Browse files
author
piggy1991
committed
pare
1 parentfa01469 commit6123720

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

‎string/LongestValidParentheses.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,38 @@ public static int longestValidParentheses(String s) {
5050
}
5151
}
5252

53+
returnmax;
54+
}
55+
publicintlongestValidParentheses2(Strings) {
56+
if (s ==null) {
57+
return0;
58+
}
59+
60+
intlen =s.length();
61+
Stack<Integer>stk =newStack<Integer>();
62+
63+
intsum =0;
64+
intmax =0;
65+
for (inti =0;i <len;i++) {
66+
charc =s.charAt(i);
67+
if (c =='(') {
68+
stk.push(i);
69+
}else {
70+
if (stk.isEmpty()) {
71+
// The sequence is cut off.
72+
sum =0;
73+
}else {
74+
inttmp =i -stk.pop() +1;
75+
if (stk.isEmpty()) {
76+
sum +=tmp;
77+
max =Math.max(max,sum);
78+
}else {
79+
max =Math.max(max,i -stk.peek());
80+
}
81+
}
82+
}
83+
}
84+
5385
returnmax;
5486
}
5587
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp