zoukankan      html  css  js  c++  java
  • leetcode81

     1 class Solution:
     2     def search(self, nums: 'List[int]', target: int) -> bool:
     3         n = len(nums)
     4         if n == 0:
     5             return False
     6         if n == 1:
     7             return target == nums[0]
     8         l,h = 0,n-1
     9         while l <= h:
    10             mid = l + (h - l) // 2
    11             if nums[l] == target or nums[mid] == target or nums[h] == target:
    12                 return True
    13             if nums[l] < nums[mid]:#左侧有序,以l为起点,以mid为终点
    14                 if nums[l] < target and target < nums[mid]:
    15                     h = mid - 1
    16                 else:
    17                     l = mid + 1
    18             elif nums[l] > nums[mid]:#右侧有序,以mid为起点,以h为终点
    19                 if nums[mid] < target and target < nums[h]:
    20                     l = mid + 1
    21                 else:
    22                     h = mid - 1
    23             else:#无法判断顺序,可以让h-1,也可以让l+1
    24                 h -= 1
    25         return False
  • 相关阅读:
    Vue过滤器使用
    vue基础
    lesson
    lesson
    lesson
    lesson
    rm 命令详解
    alias 命令详解
    cd 命令详解
    cut 命令详解
  • 原文地址:https://www.cnblogs.com/asenyang/p/12018256.html
Copyright © 2011-2022 走看看