Post

[Programmers] #181899 - 카운트 다운 [Java][C++][Python]

[Programmers] #181899 - 카운트 다운 [Java][C++][Python]

문제 링크


1. 아이디어

start_num부터 end_num까지 1씩 작아지며 두 끝을 모두 포함하므로 원소 수는 start_num - end_num + 1개이고 첫 원소는 start_num이다.


2. 코드

풀이 [Java][C++][Python]

1
2
3
4
5
6
7
8
9
10
class Solution {
    public int[] solution(int start_num, int end_num) {
        int[] arr = new int[start_num - end_num + 1];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = start_num - i;
        }

        return arr;
    }
}
1
2
3
4
5
6
7
8
9
10
11
#include <bits/stdc++.h>
using namespace std;

vector<int> solution(int start_num, int end_num) {
    vector<int> v;
    for (int i = start_num; i >= end_num; i--) {
        v.push_back(i);
    }

    return v;
}
1
2
def solution(start_num, end_num):
    return list(range(start_num, end_num - 1, -1))

range의 끝값은 결과에서 제외되므로, end_num까지 나오도록 end_num - 1을 끝값으로 넘겼다.


This post is licensed under CC BY 4.0 by the author.