반응형
1. 나머지 구하기
class Solution {
public int solution(int num1, int num2) {
return num1 % num2;
}
}
2. 중앙값 구하기
import java.util.*;
class Solution {
public int solution(int[] array) {
Arrays.sort(array);
int idx = array.length / 2;
return array[idx];
}
}
3. 최빈값 구하기
import java.util.*;
class Solution {
public int solution(int[] array) {
int maxCount = 0;
int answer = 0;
Map<Integer, Integer> map = new HashMap<>();
for(int number : array){
int count = map.getOrDefault(number, 0) + 1;
if(count > maxCount){
maxCount = count;
answer = number;
}
else if(count == maxCount){
answer = -1;
}
map.put(number, count);
}
return answer;
}
}
4. 짝수는 싫어요
class Solution {
static int[] arr;
public int[] solution(int n) {
if(n%2 == 0){
arr = new int[n/2];
}else{
arr = new int[n/2 + 1];
}
createArr(arr,n);
return arr;
}
public static void createArr(int[] arr, int n){
int idx = 0;
for(int i = 1; i<=n; i++){
if(i % 2 == 1){
arr[idx++] = i;
}
}
}
}
728x90
반응형
'CodingTest' 카테고리의 다른 글
| 프로그래머스 - 코딩테스트 입문 Day 4 (0) | 2025.04.12 |
|---|---|
| 리트 코드 - 1768. Merge Strings Alternately (0) | 2025.04.05 |
| 프로그래머스 - 코딩테스트 입문 Day 2 (0) | 2025.04.05 |
| 프로그래머스 - 코딩테스트 입문 Day 1 (0) | 2025.04.05 |
| 백준 - 가장 가까운 공통 조상 (0) | 2025.03.10 |