[프로그래머스] 배열 만들기 1

Seah Lee·2023년 6월 29일
0

프로그래머스

목록 보기
53/57

import java.util.ArrayList;

class Solution {
    public ArrayList<Integer> solution(int n, int k) {
        ArrayList<Integer> answer = new ArrayList<Integer>();
        
        for(int i=k;i<=n;i++){
            if (i%k==0) answer.add(i);
        }
        
        return answer;
    }
}

꽤나 잘 풀었다고 생각하지만 늘 int배열로는 못푸나 의문이었던 나

[다른 사람의 풀이]

class Solution {
    public int[] solution(int n, int k) {
        int count = n / k;


        int[] answer = new int[count];

        for (int i = 1; i <= count; i++) {
            answer[i - 1] = k * i;
        }

        return answer;
    }
}

처음에 저렇게 int배열 크기 맞춰두는 것 까지 해두고 어케하나 싶었는데
그냥 배수로 조질 수 있는 거였다..

profile
성장하는 개발자

0개의 댓글