백준(Backjoon) 10816, 숫자 카드 2
2021. 6. 28. 10:42ㆍCodingTest
문제풀이
각각의 정수타입 카드들을 정수형 리스트로 입력받은 다음 HashMap을 활용하여 숫자에 대해서 카운트한다.
public class Main {
public static String solution(List<Integer> my_cards, List<Integer> cards)
{
StringBuilder sb = new StringBuilder();
Map<Integer, Integer> map = new HashMap<>();
for(int card : my_cards)
{
// hashmap에 card가 포함되어 있지 않은 경우 1로 초기화
if(!map.containsKey(card))
{
map.put(card, 1);
}
else // 포함되어 있는 경우 1 누적
{
map.put(card, map.get(card)+1);
}
}
// cards 리스트의 각각의 정수에 대한 카운트 값 저장
for(int card : cards)
{
if(map.containsKey(card))
{
sb.append(map.get(card) + " ");
}
else
{
sb.append(0 + " ");
}
}
return sb.toString().trim();
}
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
List<Integer> my_cards = Arrays.asList(br.readLine().split(" ")).stream().map(Integer::parseInt).collect(Collectors.toList());
int m = Integer.parseInt(br.readLine());
List<Integer> cards = Arrays.asList(br.readLine().split(" ")).stream().map(Integer::parseInt).collect(Collectors.toList());
System.out.println(solution(my_cards, cards));
}
}
'CodingTest' 카테고리의 다른 글
백준(Backjoon) 1076, 저항 (0) | 2021.07.02 |
---|---|
백준(Backjoon) 10798, 세로읽기 (0) | 2021.06.30 |
백준(Backjoon) 2164, 카드2 (0) | 2021.06.25 |
백준(Backjoon) 10866, 덱 (0) | 2021.06.25 |
백준(Backjoon) 1966, 프린터 큐 (0) | 2021.06.25 |