상세 컨텐츠

본문 제목

카운트 업

코딩테스트/C#

by MJ_119 2024. 6. 10. 22:40

본문

문제 설명

정수 start_num와 end_num가 주어질 때, start_num부터 end_num까지의 숫자를 차례로 담은 리스트를 return하도록 solution 함수를 완성해주세요.


제한사항
  • 0 ≤ start_num  end_num ≤ 50

입출력 예

 

using System;
using System.Collections.Generic;

public class Solution {
    public List<int> solution(int start_num, int end_num) {
        
        List<int> answer = new List<int>();
        
        for(int i = start_num; i <= end_num; i++)
        {
            answer.Add(i);
        }
        
        return answer;
    }
}

 

다른 풀이

using System;

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

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

        return answer;
    }
}

 

using System;

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

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

 

 

 

'코딩테스트 > C#' 카테고리의 다른 글

간단한 논리 연산  (0) 2024.06.11
콜라츠 수열 만들기  (0) 2024.06.10
수 조작하기 2  (1) 2024.06.10
수 조작하기 1  (0) 2024.06.10
마지막 두 원소  (0) 2024.06.10

관련글 더보기