문제
내가 작성한 정답
import java.util.*;
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.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(",");
}
}
import java.util.*;
import java.util.stream.IntStream;
class Solution {
public String[] solution(String[] todoList, boolean[] finished) {
return IntStream.range(0, todoList.length) // 0부터 todoList.length 전까지의 int 스트림 생성 (인덱스 역할)
.mapToObj(i ->
// 각 인덱스 i에 대해 todoList[i]와 finished[i]의 반대 값을 쌍으로 묶어 SimpleEntry 생성
new AbstractMap.SimpleEntry<>(todoList[i], !finished[i])
)
// 값(value)이 true인 (즉, 끝나지 않은 작업) 항목만 필터링
.filter(AbstractMap.SimpleEntry::getValue)
// 필터된 항목들의 키(key)인 todoList의 문자열만 추출
.map(AbstractMap.SimpleEntry::getKey)
// 스트림을 스트링 배열로 변환하여 반환
.toArray(String[]::new);
}
}
//AbstractMap.SimpleEntry는 키-값 쌍을 간단히 저장하는 객체로,
// 여기선 (문자열, 완료여부 반대) 로 저장함으로써 끝나지 않은 작업을 표현.
Share article