코딩테스트 공부

import java.util.*;class Solution { public int solution(String[][] clothes) { int answer = 0; HashMap Hash = new HashMap(); for(String[] type : clothes){ String clothType = type[1]; if(!Hash.containsKey(clothType)){ Hash.put(clothType,1); }else{ Hash.put(clothType,Hash.get(clothType)+1); } ..
import java.util.*;class Solution { public boolean solution(String[] phone_book) { boolean answer = true; Arrays.sort(phone_book); for(int i=0; i1.Arrays.sort를 사용하여 phone_book의 순서를 정렬한다.-정렬을 해야지 1번째 순서와 2번째 순서가 인접한 번호로 정렬되기 때문에 접두사를 찾기 수월하다.2. 반복문을 통해 처음부터 마지막까지 돌린다.(i+1을 해야되기 때문에 length-1이 아닌 length를 쓰면 에러가 난다)3. startsWith 메서드를 사용하여 i+1의 문자열과 i의 문자열을 비교한다.-startsWith 메서..
2개의 String값이 있으며 두 값을 비교하여 완주하지 못한 사람을 찾는 문제이다. import java.util.*;class Solution { public String solution(String[] participant, String[] completion) { String answer = ""; HashMap map = new HashMap(); for(String player : participant){ //participant라는 입출력 내용을 player에 넣어준다. if(map.containsKey(player)){ //containsKey메서드는 해당 키가 해시맵에 있으면 true를 반환하고, 없으면 false를 반환 ..
간단하게 중복이 아닌 방법중에서 최대한 여러개를 선택해야됨(대신 전체의 절반)전체를 중복없이 선택한 값과 전체/2 중에 값이 작은걸 선택하면된다. HashSet을 쓴 방법import java.util.*;class Solution { public int solution(int[] nums) { HashSet set = new HashSet(); for(int num : nums){ set.add(num); } int answer = nums.length/2; return Math.min(set.size(),answer); }}HashMap을 쓴 방법import java...
class Solution { public double solution(int[] arr) { double answer = 0; double sum =0; int i =0; while(i < arr.length){ sum +=arr[i]; i++; } answer = sum/arr.length; return answer; } }
class Solution { public int solution(int n) { int answer = 0; for(int i=1; i
구승회
'코딩테스트 공부' 카테고리의 글 목록