코딩테스트/프로그래머스_코딩 기초 트레이닝

[프로그래머스] 카운트 다운

아리빠 2023. 5. 26. 14:13

정수 start와 end가 주어질 때, start에서 end까지 1씩 감소하는 수들을 차례로 담은 리스트를 return하도록 solution 함수를 완성해주세요

 

<생각과정>

 

 

for문 반복으로 start에서  -- 해서 end까지 가는 과정이지 않나 ..

class Solution {
    public int[] solution(int start, int end) {
        
        int[] answer = {};
        
       
        for(int answer= start; answer>=end; answer--){
            solution.add(answer);
        }
            
    
        return answer;
    }
}

는 에러 

class Solution {
    public int[] solution(int start, int end) {
        
        int[] answer = new int[answer.size];
        
        for(int i = start; i<= answer.size; i--){
            
            answer.add(i);
        }
            
        
        return answer;
    }
}

잘못한거

1. 배열 크기 선언 안해줌

2.int 타입에는 add 사용불가...

class Solution {
    public int[] solution(int start, int end) {
            
        int size = start -end +1;
        
        int answer[] = new int [size];

        for (int i = 0; i <size; i++) {
            answer[i] = start--;
        }
        
        return answer;
    }
}

 

 

★ 배열은 크기부터 선언해줘야 한다 꼭...

 

int intArray[] = new int[크기]; // 배열 선언과 동시에 생성