【每日一题20220726】二分查找

:mage:‍ 给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。

示例 1:

输入: nums = [-1,0,3,5,9,12], target = 9
输出: 4
解释: 9 出现在 nums 中并且下标为 4

示例 2:

输入: nums = [-1,0,3,5,9,12], target = 2
输出: -1
解释: 2 不存在 nums 中因此返回 -1

提示:

  • 你可以假设 nums 中的所有元素是不重复的。
  • n 将在 [1, 10000]之间。
  • nums 的每个元素都将在 [-9999, 9999]之间。

任务

编写一个函数, 执行上述操作

class Solution:
    def search(self, nums: List[int], target: int) -> int:
    # your code here

题目难度:简单
题目来源:704. 二分查找 - 力扣(LeetCode)

6666

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

image

一看就会,一写就废,这就是二分法的神奇 :rofl:

数据结构忘干净了:sob:
做出来两次,都不是二分法的思路

可以先查看答案,理解思路,再重新提交。主要的是学习解题的过程

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

assert Solution().search([-1,0,3,5,9,12],9)==4
assert Solution().search([-1,0,3,5,9,12],2)==-1
class Solution:
    def search(self, nums: list, target: int) -> int:
        nums.sort(reverse=False)
        left,right=0,len(nums)-1
        while left<=right:
            i=(left+right)//2
            if nums[i]==target:
                return i
            elif nums[i]<target:
                left=i+1
            elif nums[i]>target:
                right=i-1
        return -1

assert Solution().search([-1,0,3,5,9,12],9)==4
assert Solution().search([-1,0,3,5,6,7,9,12,13],9)==6
assert Solution().search([-1,0,3,5,6,7,9,12,13],3)==2
assert Solution().search([-1,0,3,5,7,9,12,13],3)==2
assert Solution().search([-1,0,3,5,9,12],-1)==0
assert Solution().search([-1,0,3,5,9,12],2)==-1
def test_a():
    target = 10
    nums = [-1, 0, 3, 5, 9, 12]
    len_nums = len(nums)

    for i in nums:
        continue
        if i == target:
            print(nums.index(i))
            break
        else:
            len_nums -= 1
            if len_nums == 0:
                print(-1)
                break
def test_a():
    nums = [-1, 0, 3, 5, 9, 12]
    target = 9

    left, right = 0, len(nums)-1

    while left <= right:
        m = ( left + right ) // 2
        if nums[m] == target:
            print(m)
            return m
        elif target > nums[m]:
            left = m + 1
        else:
            right = m -1
    print(-1)