[백준] 10773번: 제로 (Java)

째리·2025년 2월 10일

코테연습

목록 보기
10/11

https://www.acmicpc.net/problem/10773

내 코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int k = Integer.parseInt(br.readLine());

        List<Integer> list = new ArrayList<>(List.of());
        for(int i=0; i< k; i++){
            int n = Integer.parseInt(br.readLine());
            if ((n != 0)) {
                list.add(n);
            } else {
                list.remove(list.size()-1);
            }
        }
        int sum = 0;
        for(int l: list){
            sum += l;
        }
        System.out.println(sum);
    }
}

24352KB 204ms Java 11

스택(Stack) : LIFO
Last In First Out 마지막에 들어온 걸 우선해서 제거한다

0개의 댓글