본문 바로가기
Study with me/프로그래머스 L0 마스터하기

프로그래머스 - L0 할일목록

by 외계나무 2023. 12. 28.

프로그래머스 - level 0 할 일 목록

import java.util.*;
import java.util.stream.*;

class Solution {
    public String[] solution(String[] todo_list, boolean[] finished) {
        List<String> answer = new ArrayList<>();
        for(int i=0; i<todo_list.length; i++) {
            if(!finished[i])
                answer.add(todo_list[i]);
        }
        return answer.stream().toArray(String[]::new);
    }
}

이렇게 풀었는데, 생각치 못한 풀이를 봐서 들고옴.

class Solution {
    public String[] solution(String[] todo_list, boolean[] finished) {
        String str = "";
        for(int i=0; i<finished.length; i++){
            str = finished[i]==false ? str+todo_list[i]+"," : str;
        }

        return str.split(",");
    }
}

String으로 한번에 받은 다음에 split()으로 배열화한 값을 리턴하다니 쉬운 개념을 활용해 푼다는 건 이런거구나 싶었음.

그걸 위해 값을 추가할 때는 콤마(,)를 같이 추가한 아이디어도 좋고,

삼항 연산자 쓸 때 습관적으로 str += 로 시작해 놓고 조건이 false일 때 넣을 값이 없네... 하고 있는데, 대입연산자(=) 놓고 조건이 참이면 기존 str+추가, 거짓이면 str 그대로 다시 대입으로 한 것도 좋은 아이디어.

보면 쉬운 아이디어인데 막상 내가 풀 때는 생각나지 않는다는 점에서... 진짜 많이 풀어보고 다양하게 풀어봐야 하는 듯...