카운트 다운

nacSeo (낙서)·2024년 2월 5일
0

프로그래머스

목록 보기
69/169

문제 설명

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

제한사항

0 ≤ end_num ≤ start_num ≤ 50

나의 코드

import java.util.*;

class Solution {
    public int[] solution(int start, int end_num) {
        List<Integer> list = new ArrayList<>();
        for(int i=start; i>=end_num; i--) {
            list.add(i);
        }
        int[] answer = new int[list.size()];
        for(int j=0; j<list.size(); j++) {
            answer[j] = list.get(j);
        }
        return answer;
    }
}

다른 사람 코드

class Solution {
    public int[] solution(int start, int end) {
        int[] answer = new int[start-end+1];
        for(int i=0; i<=start-end; i++) {
            answer[i] = start - i;
        }
        return answer;
    }
}

느낀 점

나름 깔끔하게 잘 풀었다 생각했는데 배열을 선언할 때 미리 길이를 알 수 있어서 배열로만 바로 풀 수 있었다 🥲

profile
백엔드 개발자 김창하입니다 🙇‍♂️

0개의 댓글