class Solution {
public int search(int[] nums, int target) {
int high = nums.length-1;
int middle, low = 0;
while (low <= high){
middle = low + (high - low)/2;
if (nums[middle] == target){
return middle;
} else if (nums[middle] < target){
low = middle + 1;
} else {
high = middle - 1;
}
}
return -1;
}
}

'Algorithms and Data Structures > Coding Practices' 카테고리의 다른 글
LeetCode 35. Search Insert Position (0) | 2022.06.07 |
---|---|
LeetCode 374. Guess Number Higher or Lower (0) | 2022.06.04 |
CCI 4.3 List of Depths (0) | 2022.06.02 |
CCI 4.1 Route Between Nodes (0) | 2022.05.31 |
[Stacks and Queues] CCI 3.1 (0) | 2022.05.22 |