Programmers/프로그래머스-리스트 자르기-Python/Go/Java

cosmos·2023년 7월 4일
0
post-thumbnail
post-custom-banner

문제

Python Code

from typing import List

def solution(n: int, slicer: List[int], num_list: List[int]) -> List[int]:
    a, b, c = slicer
    
    if n == 1:
        return num_list[:b+1]
    elif n == 2:
        return num_list[a:]
    elif n == 3:
        return num_list[a:b+1]
    elif n == 4:
        return num_list[a:b+1:c]

Go Code

func solution(n int, slicer []int, numList []int) []int {
	a, b, c := slicer[0], slicer[1], slicer[2]

	switch n {
	case 1:
		return numList[:b+1]
	case 2:
		return numList[a:]
	case 3:
		return numList[a : b+1]
	case 4:
		var result []int
		for i := a; i <= b; i += c {
			result = append(result, numList[i])
		}
		return result
	}

	return nil
}

Java Code

import java.util.ArrayList;
import java.util.List;

class Solution {
    public static int[] solution(int n, int[] slicer, int[] numList) {
        int a = slicer[0];
        int b = slicer[1];
        int c = slicer[2];

        List<Integer> resultList = new ArrayList<>();

        switch (n) {
            case 1:
                for (int i = 0; i <= b; i++) {
                    resultList.add(numList[i]);
                }
                break;
            case 2:
                for (int i = a; i < numList.length; i++) {
                    resultList.add(numList[i]);
                }
                break;
            case 3:
                for (int i = a; i <= b; i++) {
                    resultList.add(numList[i]);
                }
                break;
            case 4:
                for (int i = a; i <= b; i += c) {
                    resultList.add(numList[i]);
                }
                break;
        }

        int[] result = new int[resultList.size()];
        for (int i = 0; i < resultList.size(); i++) {
            result[i] = resultList.get(i);
        }
        return result;
    }
}

결과

문제 출처 & 깃허브

programmers
github

post-custom-banner

0개의 댓글