문제
https://programmers.co.kr/learn/courses/30/lessons/42578
코딩테스트 연습 - 위장
programmers.co.kr
정답
import java.util.HashMap;
import java.util.Iterator;
class Solution {
public int solution(String[][] clothes) {
//String[][] clothes = [["yellowhat", "headgear"], ["bluesunglasses", "eyewear"],
["green_turban", "headgear"]]
HashMap<String, Integer> map = new HashMap<>();
for(String[] clothe : clothes) { //clothe = ["yellowhat", "headgear"]
String type = clothe[1]; //type = "headgear"
map.put(type, map.getOrDefault(type, 0) + 1);
}
Iterator<Integer> it = map.values().iterator();
int answer = 1;
while(it.hasNext()){
answer *= it.next().intValue() + 1;
}
return answer -1;
}
}
※ Iterator
-> 컬렉션에 저장되어 있는 요소들을 읽어오는 인터페이스
public interface Iterator {
boolean hasNext();
Object next();
void remove();
}
※ boolean hasNext()
-> 읽어 올 요소가 남아있는지 확인하는 메소드, true/false 반환
※ Object next()
-> 읽어 올 요소가 남아있는지 확인하는 메소드, true/false 반환
※ void remove()
-> next()로 읽어 온 요소를 삭제, next() 를 호출한 다음에 remove() 를 호출해야 함
'프로그래머스' 카테고리의 다른 글
[프로그래머스] 추억 점수 (0) | 2024.11.26 |
---|---|
[프로그래머스] 달리기 경주 (0) | 2024.11.25 |
[프로그래머스] K번째 수 (0) | 2022.03.31 |
[프로그래머스] 전화번호 목록 (JAVA) (0) | 2022.03.28 |
[프로그래머스] 완주하지 못한 선수(JAVA) (0) | 2022.03.27 |