[Leetcode]704. Binary Search

2024. 8. 2. 22:29Algorithm

1. problem : 

https://leetcode.com/problems/binary-search/

 

2. solution 1 :

class Solution:
    def search(self, nums: List[int], target: int) -> int:
        left,right = 0 , len(nums) -1 
        while left <= right:
            mid = (left + right) // 2 
            if nums[mid] < target:
                left = mid + 1 
            elif nums[mid] > target:
                right = mid - 1 
            else:
                return mid 
        return -1