Coding Test/프로그래머스

[200515] x만큼 간격이 있는 n개의 숫자 - 연습문제(level1)

csmoon1010 2020. 5. 15. 12:44

1. 문제이해

- x부터 시작해 x씩 증가하는 숫자 n개 리스트 return

 

2. 전략

- for문을 돌리며 x의 배수를 더함

  : answer[i] = x * (i+1)

 

3. 참고사항

- 다른 방법 : answer[0]을 x로 setting하고 answer[i-1]에 x를 더하기

 

4. 코드

class Solution {
    public long[] solution(long x, int n) {
        long[] answer = new long[n];
        for(int i = 0; i < n; i++){
            answer[i] = x * (i+1);
        }
        return answer;
    }
}