Skip to content

Commit 77b2c0a

Browse files
committed
refactor code
1 parent 019553a commit 77b2c0a

File tree

6 files changed

+49
-12
lines changed

6 files changed

+49
-12
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package problem12909;
2+
3+
/**
4+
* Total score: 100.0 / 100.0
5+
*/
6+
public class CorrectParenthesis_Integer {
7+
public static void main(String[] args) {
8+
var s = "()()";
9+
var sol = new CorrectParenthesis_Integer();
10+
System.out.println(sol.solution(s));
11+
}
12+
13+
boolean solution(String s) {
14+
if (s.startsWith(")") || s.endsWith("(") || s.length() % 2 != 0) {
15+
return false;
16+
}
17+
18+
var counter = 0;
19+
for (var letter : s.toCharArray()) {
20+
if (letter == '(') {
21+
counter++;
22+
} else {
23+
if (counter == 0) {
24+
return false;
25+
}
26+
counter--;
27+
}
28+
}
29+
30+
return counter == 0;
31+
}
32+
}

programmers/problem12909/CorrectParenthesis_Split.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ boolean solution(String s) {
3131
}
3232
}
3333

34-
if (!stack.isEmpty()) {
35-
return false;
36-
}
37-
return true;
34+
return stack.isEmpty();
3835
}
3936
}

programmers/problem12953/NLeastCommonMultiple.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ public int gcd(int a, int b) {
3030
return a;
3131
}
3232

33+
public int gcdRecursive(int a, int b) {
34+
if (b == 0) {
35+
return a;
36+
}
37+
return gcdRecursive(b, a % b);
38+
}
39+
3340
public int lcm(int a, int b) {
3441
return (a * b) / gcd(a, b);
3542
}

programmers/problem42628/DoubleEndedPriorityQueue_PriorityQueue.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package problem42628;
22

3-
import java.util.*;
3+
import java.util.Arrays;
4+
import java.util.Comparator;
5+
import java.util.PriorityQueue;
46

57
public class DoubleEndedPriorityQueue_PriorityQueue {
68
public static void main(String[] args) {

programmers/problem76502/RotateParentheses.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,13 @@ public int solution(String s) {
2828
var stack = new Stack<Character>();
2929

3030
for (var i = 0; i < s.length(); i++) {
31-
var temp = s.substring(i) + s.substring(0, i);
31+
var shifted = s.substring(i) + s.substring(0, i);
3232

3333
stack.clear();
3434
var isCorrect = true;
35-
for (var ch : temp.toCharArray()) {
35+
for (var ch : shifted.toCharArray()) {
3636
if (ch == '(' || ch == '[' || ch == '{') {
3737
stack.push(ch);
38-
3938
} else {
4039
if (stack.isEmpty()) {
4140
isCorrect = false;
@@ -44,6 +43,7 @@ public int solution(String s) {
4443

4544
var current = stack.peek();
4645
stack.pop();
46+
4747
if (ch == ')' && current != '(') {
4848
isCorrect = false;
4949
break;
@@ -55,7 +55,6 @@ public int solution(String s) {
5555
break;
5656
}
5757
}
58-
5958
}
6059

6160
if (stack.isEmpty() && isCorrect) count++;

programmers/problem76502/RotateParentheses_Module.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package problem76502;
22

3+
import java.util.Arrays;
34
import java.util.Stack;
45

56
public class RotateParentheses_Module {
@@ -37,13 +38,12 @@ public int solution(String s) {
3738
return count;
3839
}
3940

40-
private boolean isCorrectParentheses(String temp) {
41+
private boolean isCorrectParentheses(String shifted) {
4142
var stack = new Stack<Character>();
4243

43-
for (var ch : temp.toCharArray()) {
44+
for (var ch : shifted.toCharArray()) {
4445
if (ch == '(' || ch == '[' || ch == '{') {
4546
stack.push(ch);
46-
4747
} else {
4848
if (stack.isEmpty()) {
4949
return false;

0 commit comments

Comments
 (0)