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

Commit777f9dc

Browse files
add 1541
1 parentb037e2c commit777f9dc

File tree

3 files changed

+107
-0
lines changed

3 files changed

+107
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1541|[Minimum Insertions to Balance a Parentheses String](https://leetcode.com/problems/minimum-insertions-to-balance-a-parentheses-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1541.java)||Medium|String, Stack|
1112
|1539|[Kth Missing Positive Number](https://leetcode.com/problems/kth-missing-positive-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1539.java)||Easy|Array, HashTable|
1213
|1535|[Find the Winner of an Array Game](https://leetcode.com/problems/find-the-winner-of-an-array-game/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1535.java)|[:tv:](https://youtu.be/v6On1TQfMTU)|Medium|Array|
1314
|1534|[Count Good Triplets](https://leetcode.com/problems/count-good-triplets/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1534.java)||Easy|Array|
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
packagecom.fishercoder.solutions;
2+
3+
importjava.util.Stack;
4+
5+
publicclass_1541 {
6+
publicstaticclassSolution1 {
7+
publicintminInsertions(Strings) {
8+
Stack<Character>stack =newStack<>();
9+
intinsertionsNeeded =0;
10+
for (inti =0;i <s.length();i++) {
11+
charc =s.charAt(i);
12+
if (c =='(') {
13+
if (stack.isEmpty()) {
14+
stack.add(c);
15+
}else {
16+
if (stack.peek() ==')') {
17+
//in this case, we need to add one more ')' to get two consecutive right paren, then we could pop the one ')' and one '(' off the stack
18+
insertionsNeeded++;
19+
stack.pop();
20+
stack.pop();
21+
stack.add(c);
22+
}else {
23+
stack.add(c);
24+
}
25+
}
26+
}elseif (c ==')') {
27+
if (stack.isEmpty()) {
28+
//in this case, we need to add one '(' before we add this ')' onto this stack
29+
insertionsNeeded++;
30+
stack.add('(');
31+
stack.add(c);
32+
}else {
33+
if (stack.peek() ==')') {
34+
//in this case, we could pop the one ')' and one '(' off the stack
35+
stack.pop();
36+
stack.pop();
37+
}else {
38+
stack.add(c);
39+
}
40+
}
41+
}
42+
}
43+
if (stack.isEmpty()) {
44+
returninsertionsNeeded;
45+
}else {
46+
while (!stack.isEmpty()) {
47+
charpop =stack.pop();
48+
if (pop =='(') {
49+
insertionsNeeded +=2;
50+
}else {
51+
insertionsNeeded++;
52+
stack.pop();
53+
}
54+
}
55+
returninsertionsNeeded;
56+
}
57+
}
58+
}
59+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._1541;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_1541Test {
10+
privatestatic_1541.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_1541.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(1,solution1.minInsertions("(()))"));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(0,solution1.minInsertions("())"));
25+
}
26+
27+
@Test
28+
publicvoidtest3() {
29+
assertEquals(3,solution1.minInsertions("))())("));
30+
}
31+
32+
@Test
33+
publicvoidtest4() {
34+
assertEquals(12,solution1.minInsertions("(((((("));
35+
}
36+
37+
@Test
38+
publicvoidtest5() {
39+
assertEquals(5,solution1.minInsertions(")))))))"));
40+
}
41+
42+
@Test
43+
publicvoidtest6() {
44+
assertEquals(4,solution1.minInsertions("(()))(()))()())))"));
45+
}
46+
47+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp