728x90
나의풀이
class Solution { public long[] solution(int x, int n) { long[] answer = new long[n]; long val=x; for(int i=0;i<n;i++) { answer[i]= val; val +=x; } return answer; } } |
느낀점 : new하는것을 자꾸 까먹고 프로그래밍을 한다.
자주 틀리는 new와 조건 확인을 상기 시키자!!
베스트코드(프로그래머스 출처)
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; } } |
고찰 : 현재 값 = 이전값 + x값 마치 DP같이 사용을했다... 역시 좋은 코드다.
728x90
'프로그래머스' 카테고리의 다른 글
문자열 다루기 기본 (0) | 2022.12.21 |
---|---|
두 정수 사이의 합 (0) | 2022.12.19 |
핸드폰번호가리기 (0) | 2022.12.19 |
1차 비밀지도 카카오 (0) | 2022.12.11 |
문자열 내마음대로 정렬하기 (0) | 2022.12.11 |