개발하는 고양이 오이

16. [JAVA] 프로그래머스 코딩테스트 연습 Level.1 - x만큼 간격이 있는 n개의 숫자 / 13,14번 실패 해결 본문

코딩테스트 - JAVA/프로그래머스 Programmers

16. [JAVA] 프로그래머스 코딩테스트 연습 Level.1 - x만큼 간격이 있는 n개의 숫자 / 13,14번 실패 해결

Cucum 2022. 4. 29. 12:13

https://programmers.co.kr/learn/challenges

 

코딩테스트 연습

기초부터 차근차근, 직접 코드를 작성해 보세요.

programmers.co.kr

 

프로그래머스 코딩테스트 연습 Level.1 - x만큼 간격이 있는 n개의 숫자



풀이

class Solution {
    public long[] solution(long x, int n) {
        long[] answer = new long[n];
        
        for (int i = 1; i <= n; i++) {
            answer[i - 1] = (x * i);
        }
        return answer;
    }
}

 


(풀이 실패 과정)

class Solution {
    public long[] solution(int x, int n) {
        long[] answer = new long[n];
        
        for (int i = 1; i <= n; i++) {
            answer[i - 1] = (x * i);
        }
        return answer;
    }
}

 

처음에 long형 변수를 생각해주지 않아서 테스트 13, 14번 실패

-> int x를 long x로 변경 후 성공