알고리즘
[프로그래머스] x만큼 간격이 있는 n개의 숫자(Java)
muerha
2024. 10. 31. 14:50

나의 풀이
class Solution {
public long[] solution(int x, int n) {
long[] answer = new long[n];
for(int i = 1; i <= n ; i++ ){
answer[i - 1] = (long) x * i;
}
return answer;
}
}
배열을 n길이로 초기화 한 후에 1부터 n까지 반복하게 하고
long 타입으로 변환한 값을 배열의 i - 1 번째 위치에 저장했다.
다른 사람의 풀이 1
class Solution {
public 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;
}
}
먼저 배열의 첫 번째 요소에 x 값을 넣고 시작하는 방법 ..
다른 사람의 풀이 2
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;
}
}