Post

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

start_num부터 end_num까지 정수를 순서대로 나열하는 워밍업 문제.

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

문제 링크


1. 아이디어

start_num부터 end_num까지 정수를 하나씩 순서대로 담아 배열로 반환하면 되는 문제다. 범위를 그대로 순회하는 반복문 하나로 충분하다.


2. 복잡도

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

($N$ = 반환할 배열의 길이)


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[] ans = new int[end_num - start_num + 1];
        for (int i = 0; i < ans.length; i++) {
            ans[i] = start_num + i;
        }

        return ans;
    }
}
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))

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