From 09961c57b4074bc80da02b29966a3bd521447143 Mon Sep 17 00:00:00 2001 From: Chirag <55131799+Chirag-007@users.noreply.github.com> Date: Wed, 9 Mar 2022 12:27:31 +0530 Subject: [PATCH] Binary.py --- LeetCode/BinarySearch.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 LeetCode/BinarySearch.py diff --git a/LeetCode/BinarySearch.py b/LeetCode/BinarySearch.py new file mode 100644 index 0000000..a5824c1 --- /dev/null +++ b/LeetCode/BinarySearch.py @@ -0,0 +1,16 @@ +#Question Link : https://leetcode.com/problems/binary-search/ +class Solution: + def search(self, nums: list[int], target: int) -> int: + left = 0 + right = len(nums)-1 + while left < right: + mid = left + (right-left)//2 + if nums[mid] == target: + return mid + if nums[mid] < target: + left = mid + 1 + if nums[mid] > target: + right = mid -1 + if nums[left] == target: + return left + return -1