https://school.programmers.co.kr/learn/courses/30/lessons/12910
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
구현을 통해 해당 문제를 해결하였습니다.
먼저 배열을 오름차순으로 정렬 후 divisor로 나누어 떨어지는지 확인하고 List에 넣어주었습니다.
만약 리스트의 사이즈가 0 이라면 -1을 넣어주고 결과 배열에 넣었습니다.
import java.util.*;
class Solution {
private static final int NOT_FOUND = -1;
public int[] solution(int[] arr, int divisor) {
int[] answer = {};
List<Integer> result = new LinkedList<>();
Arrays.sort(arr);
for(int num : arr){
if(num % divisor == 0){
result.add(num);
}
}
if(result.size() == 0){
result.add(NOT_FOUND);
}
answer = new int[result.size()];
for(int i = 0 ; i < result.size(); i++){
answer[i] = result.get(i);
}
return answer;
}
}
'알고리즘' 카테고리의 다른 글
백준 2151번 거울 설치 (JAVA) (0) | 2022.07.07 |
---|---|
프로그래머스 소수 찾기 (JAVA) (0) | 2022.07.06 |
백준 10162번 전자레인지 (JAVA) (0) | 2022.07.04 |
프로그래머스 하샤드 수 (JAVA) (0) | 2022.07.03 |
백준 2015번 수들의 합4 (JAVA) (0) | 2022.07.02 |