공부블로그
[프로그래머스] 로또의 최고 순위와 최저 순위 본문
https://programmers.co.kr/learn/courses/30/lessons/77484
코딩테스트 연습 - 로또의 최고 순위와 최저 순위
로또 6/45(이하 '로또'로 표기)는 1부터 45까지의 숫자 중 6개를 찍어서 맞히는 대표적인 복권입니다. 아래는 로또의 순위를 정하는 방식입니다. 1 순위 당첨 내용 1 6개 번호가 모두 일치 2 5개 번호
programmers.co.kr
import java.util.*;
class Solution {
public int[] solution(int[] lottos, int[] win_nums) {
int count=0;
int[] answer = new int[2];
HashSet<Integer> lotto = new HashSet<Integer>();
HashSet<Integer> win = new HashSet<Integer>();
for(int n: lottos) {
if(n==0) count++;
lotto.add(n);
}
for(int n: win_nums){
win.add(n);
}
lotto.retainAll(win);
int correct = lotto.size(); //맞힌 개수
for(int i=0; i<answer.length; i++){ //answer[0], [1]
int num=7;
if(i==0) num -= (correct + count); //최고 순위
else num -= correct; //최저 순위
if(num==7) num=6;
answer[i]=num;
}
return answer;
}
}
알게 된 점
- Arrays.asList()를 이용해 배열을 Set으로 변경하려고 했으나 오류.
이유:
lottos, win_nums 배열은 int[ ]형이라 위 메서드를 적용시키면 List<int[ ]>을 반환.
으로 HashSet<Integer>(Collection<? extends Integer> c)이나 다름 없으나 List<int[ ]>에서 int[]은 <- ->Integer/ Object변환이 불가능하기 때문에 오류가 났음.
따라서 Integer[ ]을 asList()의 매개변수로 주면 사용가능.
'IT > 알고리즘' 카테고리의 다른 글
[프로그래머스] 1차 비밀지도 (0) | 2022.07.07 |
---|---|
[프로그래머스] 신고결과 받기 (0) | 2022.06.18 |
[프로그래머스] 크레인 인형뽑기 (0) | 2022.06.15 |
[프로그래머스] 3진법 뒤집기 (0) | 2022.06.11 |
[프로그래머스] 모의고사 (0) | 2022.06.11 |
Comments