-
[프로그래머스] 2의 영역알고리즘 노트 2023. 10. 23. 11:36
주어진 배열에서 제일 처음 나오는 2와 가장 마지막에 나오는 2의 중간 배열 값을 구하면 되는 문제입니다.
추가적으로 2가 없다면 -1을 리턴하라고 하네요.
[문제풀이]
1. IntStream의 range함수를 통해 배열의 요소값이 2인 index를 구합니다.
2. 구해진 index배열의 길이가 0이라면 -1을 바로 반환합니다.
3. 그렇지 않다면 IntStream의 rangeClosed함수로 index의 첫번째 값부터 index의 마지막 값까지를 arr의 인덱스 값으로 활용하여 int형 배열을 반환합니다.
'알고리즘 노트' 카테고리의 다른 글
[프로그래머스] 왼쪽 오른쪽 (0) 2023.10.24 [프로그래머스] 배열 조각하기 (0) 2023.10.23 [프로그래머스] 배열 만들기 3 (0) 2023.10.23 [프로그래머스] 카운트 다운 (0) 2023.10.20 [프로그래머스] 글자 지우기 (0) 2023.10.20