Created
April 12, 2024 22:38
-
-
Save kunal768/66856c3d2aaf02ccdbdd2eb4e5b04e22 to your computer and use it in GitHub Desktop.
Binary Search - New Way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def search(self, nums: List[int], target: int) -> int: | |
n = len(nums) | |
lo, hi = 0, n - 1 | |
while hi - lo > 1 : | |
mid = (lo + hi)//2 | |
if nums[mid] < target : | |
lo = mid + 1 | |
else: | |
hi = mid | |
if nums[lo] == target : return lo | |
elif nums[hi] == target : return hi | |
return -1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment