본문 바로가기
프로그래머스/[프로그래머스 - JAVA] Lv.1

[프로그래머스 - JAVA] x만큼 간격이 있는 n개의 숫자

by 코딩하는 흰둥이 2023. 3. 26.
반응형


  • 내 풀이
class Solution {
    public long[] solution(int x, int n) {
        long[] answer = {};
        
        answer = new long[n];

        int sum = 0;
        for (int i = 1; i <= n; i++) {
        // (long)i 로 형변환을 하지 않으면 13,14 번 테스트에서 오류가 생긴다
            answer[i -1] = (long)i * x;
        }
        return answer;
    }
}

 

  • 다른 사람 풀이
import java.util.*;
class Solution {
    public static long[] solution(int x, int n) {
        long[] answer = new long[n];
        answer[0] = x;

        for (int i = 1; i < n; i++) {
            answer[i] = answer[i - 1] + x;
        }

        return answer;

    }
}

댓글