프로그래머스
n의 배수 찾기
backend_fighting
2022. 12. 5. 11:09
728x90
import static java.util.Arrays.sort;
import java.util.ArrayList;
import java.util.List;
class Solution
{
public static int[] solution(int n, int[] numlist)
{
int[] answer = {};
int cnt=0;
List<Integer> calcul = new ArrayList<Integer>();
for(int i=0;i<numlist.length;i++)
{
if(numlist[i]%n==0)
{
calcul.add(numlist[i]);
cnt++;
}
}
answer = calcul.stream().mapToInt(Integer::intValue).toArray();
return answer;
}
}
생각 흐름 : 처음에는 while과 for문을 돌려서 무식하게 n^2만큼의 시간복잡도가 나오게 막짰는데 시간 초과가 나서... 나중에 다른방법을 생각해보니 %연산자가 생각나서 적용시켰더니 통과 했다.
728x90