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

Commitf572910

Browse files
add a solution for 32
1 parent43ccd93 commitf572910

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

‎src/main/java/com/fishercoder/solutions/_32.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,50 @@ public int longestValidParentheses(String s) {
2323
returnresult;
2424
}
2525
}
26+
27+
publicstaticclassSolution2 {
28+
/**
29+
* my lengthy but original solution on 4/5/2021, the idea is to convert the valid parenthesis pairs into numbers and push them onto a stack.
30+
*/
31+
publicintlongestValidParentheses(Strings) {
32+
Stack<String>stack =newStack<>();
33+
intlongest =0;
34+
for (charc :s.toCharArray()) {
35+
if (c =='(') {
36+
stack.push(c +"");
37+
}else {
38+
if (stack.isEmpty()) {
39+
continue;
40+
}else {
41+
if (stack.peek().equals("(")) {
42+
stack.pop();
43+
stack.push("2");
44+
}else {
45+
intsum =0;
46+
while (!stack.isEmpty() && !stack.peek().equals("(")) {
47+
sum +=Integer.parseInt(stack.pop());
48+
}
49+
if (!stack.isEmpty()) {
50+
stack.pop();//pop off the open paren
51+
sum +=2;
52+
stack.push("" +sum);
53+
}
54+
longest =Math.max(longest,sum);
55+
}
56+
}
57+
}
58+
}
59+
intresult =0;
60+
while (!stack.isEmpty()) {
61+
if (stack.peek().equals("(")) {
62+
stack.pop();
63+
result =0;
64+
}else {
65+
result +=Integer.parseInt(stack.pop());
66+
longest =Math.max(result,longest);
67+
}
68+
}
69+
returnlongest;
70+
}
71+
}
2672
}

‎src/test/java/com/fishercoder/_32Test.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,60 @@
88

99
publicclass_32Test {
1010
privatestatic_32.Solution1solution1;
11+
privatestatic_32.Solution2solution2;
1112

1213
@BeforeClass
1314
publicstaticvoidsetup() {
1415
solution1 =new_32.Solution1();
16+
solution2 =new_32.Solution2();
1517
}
1618

1719
@Test
1820
publicvoidtest1() {
1921
assertEquals(2,solution1.longestValidParentheses("(()"));
22+
assertEquals(2,solution2.longestValidParentheses("(()"));
2023
}
24+
25+
@Test
26+
publicvoidtest2() {
27+
assertEquals(2,solution1.longestValidParentheses("()(()"));
28+
assertEquals(2,solution2.longestValidParentheses("()(()"));
29+
}
30+
31+
@Test
32+
publicvoidtest3() {
33+
assertEquals(4,solution1.longestValidParentheses("(())("));
34+
assertEquals(4,solution2.longestValidParentheses("(())("));
35+
}
36+
37+
@Test
38+
publicvoidtest4() {
39+
assertEquals(6,solution1.longestValidParentheses("()(())"));
40+
assertEquals(6,solution2.longestValidParentheses("()(())"));
41+
}
42+
43+
@Test
44+
publicvoidtest5() {
45+
assertEquals(4,solution1.longestValidParentheses(")()())"));
46+
assertEquals(4,solution2.longestValidParentheses(")()())"));
47+
}
48+
49+
@Test
50+
publicvoidtest6() {
51+
assertEquals(4,solution1.longestValidParentheses(")()())()()("));
52+
assertEquals(4,solution2.longestValidParentheses(")()())()()("));
53+
}
54+
55+
@Test
56+
publicvoidtest7() {
57+
assertEquals(8,solution1.longestValidParentheses("((())())"));
58+
assertEquals(8,solution2.longestValidParentheses("((())())"));
59+
}
60+
61+
@Test
62+
publicvoidtest8() {
63+
assertEquals(10,solution1.longestValidParentheses(")()(((())))("));
64+
assertEquals(10,solution2.longestValidParentheses(")()(((())))("));
65+
}
66+
2167
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp