Post

[Programmers] #181920 - 카운트 업 [Java][C++][Python]

[Programmers] #181920 - 카운트 업 [Java][C++][Python]

문제 링크


1. 아이디어

start_num부터 end_num까지 정수를 하나씩 순서대로 담아 배열로 반환하면 되는 간단한 문제다.


2. 복잡도

접근시간공간
풀이$O(N)$$O(N)$

($N$ = end_num - start_num + 1)


3. 코드

풀이 [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[end_num - start_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
#include <bits/stdc++.h>
using namespace std;

vector<int> solution(int start_num, int end_num) {
    vector<int> v(end_num - start_num + 1);
    iota(v.begin(), v.end(), start_num);
    return v;
}
1
2
def solution(start_num, end_num):
    return list(range(start_num, end_num + 1))

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