프로그래머스 1단계
나누어 떨어지는 숫자 배열
https://programmers.co.kr/learn/challenges
문제 설명
풀이
import java.util.*;
class Solution {
public int[] solution(int[] arr, int divisor) {
ArrayList<Integer> answerList = new ArrayList<Integer>();
for(int i=0; i<arr.length; i++) {
if(arr[i] % divisor == 0) {
System.out.println(arr[i]);
answerList.add(arr[i]);
}
}
if(answerList.isEmpty()) {
answerList.add(-1);
}
int[] answer = new int[answerList.size()];
for(int i=0; i<answer.length; i++) {
answer[i] = answerList.get(i);
}
Arrays.sort(answer);
return answer;
}
}
'알고리즘 > 프로그래머스 1단계' 카테고리의 다른 글
[프로그래머스] 비밀지도 (0) | 2022.04.25 |
---|---|
[프로그래머스] 문자열 내 마음대로 정렬하기 (0) | 2022.04.25 |
[프로그래머스] 예산 (0) | 2022.04.24 |
[프로그래머스] 모의고사 (0) | 2022.04.24 |
[프로그래머스] 직사각형 별찍기 (0) | 2022.04.24 |