[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์ฌ๋ฐ๋ฅธ ๊ดํธ (์๋ฐ java)
1. ๋ฌธ์ : https://school.programmers.co.kr/learn/courses/30/lessons/12909
2. ํ์ด
1) count๋ฅผ ํ์ฉํ ํ์ด
2) stack์ ํ์ฉํ ํ์ด
3. ์ฝ๋
1)
class Solution {
boolean solution (String s) {
boolean answer = false;
int count = 0;
for (int i = 0; i < s.length(); i++) {
if(s.charAt (i) =='(') count++;
else count--;
if (count < 0) break;
}
if(count == 0) answer = true;
return answer;
}
}
2)
import java.util.*;
class Solution {
boolean solution(String s) {
boolean answer = true;
Stack<Character> stack = new Stack<>();
for(int i = 0; i < s.length(); i++){
char c = s.charAt(i);
//์ฌ๋ ๊ดํธ์ผ ๋
if(c == '('){
stack.push(c);
}
//๋ซ๋ ๊ดํธ์ผ ๋
if(c == ')'){
if(stack.size() == 0){
return false;
}
else stack.pop();
}
}
if(stack.size() != 0) answer = false;
return answer;
}
}
'๐ป Coding Problems Solving > Stack | Queue' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ 11000] ๊ฐ์์ค ๋ฐฐ์ (0) | 2023.05.25 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ํ๋ฆฐํฐ (์๋ฐ java) (0) | 2023.02.22 |
[BOJ 2504] ๊ดํธ์ ๊ฐ (0) | 2022.06.26 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ๋ค๋ฆฌ๋ฅผ ์ง๋๋ ํธ๋ญ (0) | 2022.04.15 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์ง์ง์ด ์ ๊ฑฐํ๊ธฐ (0) | 2022.04.09 |
์ต๊ทผ๋๊ธ