
나의 풀이
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;
}
}
'알고리즘' 카테고리의 다른 글
| [프로그래머스] 배열 두 배 만들기 (Java) (0) | 2024.11.01 |
|---|---|
| [프로그래머스] 자연수 뒤집어 배열로 만들기(Java) * (0) | 2024.11.01 |
| [프로그래머스] 나머지가 1이 되는 수 찾기(Java) (0) | 2024.10.30 |
| [프로그래머스] 약수의 합(Java) (0) | 2024.10.29 |
| [프로그래머스] 평균 구하기(Java) (0) | 2024.10.28 |