백준 / 10815 / 숫자 카드 / java

맹민재·2023년 5월 4일
0

Java

목록 보기
6/32
package backjun.F집합과맵;

import java.util.Scanner;
import java.util.HashMap;

public class 숫자카드 {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        HashMap<Integer, Integer> map = new HashMap<>();
        for(int i=0; i<n;i++){
            int num = sc.nextInt();
            map.put(num, 0);
        }
        int m = sc.nextInt();
        StringBuffer bf = new StringBuffer();
        for(int i=0; i<m;i++){
            int t = sc.nextInt();
            if(map.containsKey(t))
                bf.append(1 + " ");
            else
                bf.append(0 + " ");
        }
        System.out.print(bf);

    }
    
}

Map(딕셔너리) 자료구조로 해결한 문제
Map은 Key 값을 탐색하느데 O(1)의 시간 복잡도를 갖는다. 따라서 이 문제는 Map을 통해 해결할 수 있다.

profile
ㄱH ㅂrㄹ ㅈr

0개의 댓글