[알고리즘] Search for a Range
asc로 정렬된 Integer배열에서 지정된 범위의 target Value를 찾아라
알고리즘의 복잡도는 O(log n)이어야 하며,
target Value를 찾지 못하면 [-1, -1]을 반환한다.
Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.
Your algorithm’s runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1]
.
For example,
Given [5, 7, 7, 8, 8, 10]
and target value 8,
return [3, 4]
.
class Solution { public int[] searchRange(int[] nums, int target) { int startIndex = -1; int endIndex = -1; for(int i=0; i<nums.length; i++){ if(nums[i] == target){ if(startIndex == -1){ startIndex = i; } endIndex = i; } } return new int[]{startIndex, endIndex}; } }
최근 댓글