Nesting
Determine whether a given string of parentheses (single type) is properly nested.
Task Score
100%
Correctness
100%
Performance
100%
Brackets랑 다른게 뭔데 ㅠ
똑같이 풀어도 똑같이 100퍼 나오는거 뭔데ㅠ..
그냥 if문 조건만 if(A){} if(B){}에서 if(A||B){}로만 바꿨다..ㅎ
x
// you can also use imports, for example:import java.util.*;// you can write to stdout for debugging purposes, e.g.// System.out.println("this is a debug message");class Solution { public int solution(String S) { Stack<Character> stack = new Stack<>(); for (char c : S.toCharArray()) { if (c == '(') { stack.add(c); } else if (stack.size() == 0 || stack.pop() == c) { return 0; } } if (stack.size() > 0) return 0; return 1; }}
는 너무 찝찝해서 스택안쓰고도 한 번 풀어봤다.
xxxxxxxxxx// you can also use imports, for example:// import java.util.*;// you can write to stdout for debugging purposes, e.g.// System.out.println("this is a debug message");class Solution { public int solution(String S) { int sum = 0; for (char c : S.toCharArray()) { if (c == '(') sum++; else if (sum > 0) sum--; else return 0; } if (sum > 0) return 0; return 1; }}
스택 넣었다뺐다 하는걸 카운트로 넣었다뺐다 하는 것으로 변경했다.
뭐 어떤 클래스를 사용했느냐가 다를 뿐
점수도같고 복잡도도 같게 나왔다.
'공부 > algorithm' 카테고리의 다른 글
| [codility] Dominator (0) | 2020.01.06 |
|---|---|
| [codility] StoneWall (0) | 2019.12.22 |
| [codility] Fish (0) | 2019.12.12 |
| [codility] Brackets (0) | 2019.12.11 |
| [LeetCode] #4 Median of Two Sorted Arrays (0) | 2019.12.08 |
댓글