상세 컨텐츠

본문 제목

카운트 다운

코딩테스트/C#

by MJ_119 2024. 6. 12. 23:37

본문

문제 설명

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


제한사항
  • 0 ≤ end_num  start_num ≤ 50

입출력 예

 

 

using System;

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

 

다른 풀이

using System;
using System.Linq;

public class Solution {
    public int[] solution(int start, int end) {
        return Enumerable.Range(end, start - end + 1).Reverse().ToArray();
    }
}

 

using System;
using System.Collections.Generic;

public class Solution {
    public int[] solution(int start, int end) {
        List<int> answer = new List<int>();
        for(int i = start; i >= end; i--){
            answer.Add(i);

        }

        return answer.ToArray();
    }
}

 

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

1로 만들기  (0) 2024.06.13
가까운 1 찾기  (0) 2024.06.12
글자 지우기  (0) 2024.06.12
배열 만들기 1  (0) 2024.06.12
문자열의 앞의 n글자  (0) 2024.06.11

관련글 더보기