카테고리 없음
[codility] Nesting
by 댓츠굿
2018. 8. 27.
문제)
정답)
| /* |
| * Complexity: |
| * expected worst-case time complexity is O(N); |
| * expected worst-case space complexity is O(1)
* (not counting the storage required for input arguments). |
| * |
| * my answer URL : https://app.codility.com/demo/results/trainingPWC93R-SRE/ |
| */ |
|
|
| class Nesting { |
| public int solution(String S) {
//The result of this algorithm is the same, whether there is this clause or not.
|
| //if (S.isEmpty()) return 1;
|
| |
| int counter = 0; |
| for (int i = 0; i < S.length(); i++) { |
| if (S.charAt(i) == '(') |
| counter++; |
| if (S.charAt(i) == ')') |
| counter--; |
| if (counter < 0) //If String S has ")))", counter is -3. |
| return 0; |
| } |
|
|
| return counter == 0 ? 1 : 0; |
| } |
| } |
- 내 깃헙 주소 : https://github.com/boniato/algorithms/commit/de78c8e0ef8b0e6bdbdc60f412637c5e2047b0cd