프로그래머스
x만큼 간격이 있는 n개의 숫자
backend_fighting
2022. 12. 19. 21:19
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