본문 바로가기
Algorithm/Programmers

[Programmers] Lv.0 / 잘라서 배열로 저장하기 / Java

by unknownomad 2024. 3. 20.

문제

 

풀이

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

class Solution {
    public String[] solution(String my_str, int n) {
        
        List<String> resultList = new ArrayList<>();
        int length = my_str.length();
        
        for(int i = 0; i < length; i += n) {
            resultList.add(my_str.substring(i, Math.min(i + n, length)));
        }
        return resultList.toArray(new String[0]);
    }
}

 

출처

https://school.programmers.co.kr/learn/courses/30/lessons/120913

댓글