· 문제 설명
함수 solution은 정수 x와 자연수 n을 입력 받아, x부터 시작해 x씩 증가하는 숫자를 n개 지니는 리스트를 리턴해야 합니다. 다음 제한 조건을 보고, 조건을 만족하는 함수, solution을 완성해주세요.
· 제한 조건
x는 -10000000 이상, 10000000 이하인 정수입니다.
n은 1000 이하인 자연수입니다.
· 입출력 예
x | n | answer |
2 | 5 | [2, 4, 6, 8, 10] |
4 | 3 | [4, 8, 12] |
-4 | 2 | [-4, -8] |
· Thinking 1
- int x, int n -> long[] answer
class Solution {
public long[] solution(int x, int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
int answer = x * (i+1);
arr[i] = answer;
}
return arr;
}
}
- 정확성 85.7
- answer을 long타입으로, 뒤에 L 단위 붙여주기..!
· 완성 코드
class Solution {
public long[] solution(int x, int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
long answer = x * (i+1L);
arr[i] = answer;
}
return arr;
}
}
· 문제 출처
https://programmers.co.kr/learn/courses/30/lessons/12954
'Algorithm > Programmers' 카테고리의 다른 글
나누어 떨어지는 숫자 배열 (0) | 2019.05.31 |
---|---|
같은 숫자는 싫어 (0) | 2019.05.31 |
가운데 글자 가져오기 (0) | 2019.03.26 |
K번째 수 (1) | 2019.03.26 |
2016년 (0) | 2019.03.26 |
댓글