처음 생각
보자마자 이건 스택이다! 라고 생각했다
그리고 스택 사용할 때는 스택 안에 값이 남아있는지 없는지 확인하는 것에 유의하자!
내가 푼 방법
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
Stack<Integer> number = new Stack<>();
int total=0;
for(int i=0 ;i <T; i++) {
int num = sc.nextInt();
if(num == 0 && !number.isEmpty()) {
number.pop();
}
else {
number.add(num);
}
}
for(int a : number) {
total += a;
}
System.out.println(total);
}
}
'백준 풀이' 카테고리의 다른 글
[백준 풀이_Java] 9012 괄호 (실버4) (1) | 2023.11.13 |
---|---|
[백준 풀이_Java] 2941 크로아티아 알파벳 (0) | 2021.10.31 |
[백준 풀이_Java] 2292 벌집 (0) | 2021.10.29 |
[백준 풀이_Java] 2875 대회 or 인턴 (0) | 2021.10.28 |
[백준 풀이_Java] 7568 덩치 (0) | 2021.10.28 |