본문 바로가기

프로그래머스

n의 배수 찾기

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

'프로그래머스' 카테고리의 다른 글

안전지대  (0) 2022.12.08
OX퀴즈  (0) 2022.12.08
영어가 싫어요  (0) 2022.12.06
캐릭터의 좌표  (0) 2022.12.06
2차원으로 만들기  (0) 2022.12.05