Previous
Previous
Sliding Window
Next
Next
# If the target exists, returns its leftmost index. | |
# Else, returns the index of where it should be. | |
def binarySearch(nums: List[int], target: int) -> int: | |
l, r = 0, len(nums) | |
while l < r : | |
m = (l + r) // 2; | |
if nums[m] < target: | |
l = m + 1; | |
else: | |
r = m; | |
return l; |
// If the target exists, returns its leftmost index. | |
// Else, returns the index of where it should be. | |
int binarySearch(int[] nums, int target) { | |
int l = 0, r = nums.length; | |
while (l < r) { | |
int m = (l + r) / 2; | |
if (nums[m] < target) l = m + 1; | |
else r = m; | |
} | |
return l; | |
} |
Tricks