하샤드 수
- step1 - 자릿수의 합 구하기
- step2 - 나누어 떨어지는지 check하기
public class Solutuon {
public boolean solution(int x) {
int sumOfDigit = 0;
while (x > 0) {
sumOfDigit += x % 10;
x = x / 10;
}
if ((x % sumOfDigit) == 0) {
return true;
}
return false;
}
나누어 떨어지는 숫자 배열
solution1
public class Solution {
public int[] solution (int [] arr , int divisor) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
if ((arr[i] / divisor) == 0) {
list.add(arr[i]);
}
if (list.size() == 0) return new int[]{-1};
}
int[] answer = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
answer[i] = list.get(i);
}
Arrays.sort(answer);
return answer;
}
}
solution2
public class Solution2 {
public int[] solution(int[] arr, int divisor) {
PriorityQueue<Integer> list = new PriorityQueue<>();
for (int i = 0; i < arr.length; i++) {
if ((arr[i] / divisor) == 0) list.add(arr[i]);
}
if (list.size() == 0) return new int[]{-1};
int[] answer = new int[list.size()];
int idx = 0;
while (!list.isEmpty()) {
answer[idx++] = list.poll();
}
return answer;
}
}