백준 11723번 (2회차)

김경욱·2025년 9월 29일

백준

목록 보기
90/121

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.util.*;

import static java.util.Collections.*;

public class Main {
public static void main(String[] args) throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    int N = Integer.parseInt(br.readLine());

    HashSet<Integer> hash = new HashSet<>();
    StringBuilder sb = new StringBuilder();


    for (int i = 0 ; i < N; i++)
    {
        StringTokenizer st = new StringTokenizer(br.readLine());

        String function = st.nextToken();
        if (function.equals("all"))
        {
            for(int j=1; j<=20; j++)
            {
              hash.add(j);
            }
            continue;
        }



        if (function.equals("empty"))
        {
            hash.clear();
            continue;
        }




        int M = Integer.parseInt(st.nextToken());

        if (function.equals("add"))
        {
            hash.add(M);

        }

        if (function.equals("check"))
        {
            if (hash.contains(M))
            {
                sb.append("1\n");

            }
            else{
                sb.append("0\n");

            }
        }

        if (function.equals("remove"))
        {
            hash.remove(M);

        }

        if (function.equals("toggle"))
        {
            if (hash.contains(M))
            {
                hash.remove(M);

            }
            else{
                hash.add(M);

            }
        }





    }


    System.out.println(sb);






}
}

hash함수를 다 비우려면 .clear()메서드를 사용한다, StringBuilder를 사용하여 시간을 줄여야 한다.

0개의 댓글