-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
86 lines (69 loc) · 4.19 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package Programmers.올바른_괄호;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.stream.Stream;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
// https://programmers.co.kr/learn/courses/30/lessons/12909
// 푼 날짜 : 220426
public class Solution {
private boolean 나의_풀이_charAt(String str) {
int pivot = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '(') {
pivot++;
} else if (str.charAt(i) == ')') {
pivot--;
}
if (pivot < 0) {
return false;
}
}
if (pivot != 0) {
return false;
}
return true;
}
private boolean 나의_풀이_split(String str) {
String[] strings = str.split("");
int pivot = 0;
for (int i = 0; i < strings.length; i++) {
if (strings[i].equals("(")) {
pivot++;
} else if (strings[i].equals(")")) {
pivot--;
}
if (pivot < 0) {
return false;
}
}
if (pivot != 0) {
return false;
}
return true;
}
@ParameterizedTest
@MethodSource("parameterProvider")
public void solution(String str, boolean result) {
// given & when
long start = System.currentTimeMillis();
boolean answer = 나의_풀이_charAt(str);
long end = System.currentTimeMillis();
System.out.println("charAt 수행시간: " + (end - start) + " ms");
start = System.currentTimeMillis();
boolean answer2 = 나의_풀이_split(str);
end = System.currentTimeMillis();
System.out.println("split 수행시간: " + (end - start) + " ms");
// then
assertEquals(answer, result);
}
private static Stream<Arguments> parameterProvider() {
return Stream.of(
Arguments.arguments("()()", true),
Arguments.arguments("(())()", true),
Arguments.arguments(")()(", false),
Arguments.arguments("(()(", false),
Arguments.arguments("()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()()", true)
);
}
}