Table of Content
题目如下:
这道题比较简单,不做过多解释了,注意程序终止条件,参考代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
class Solution: def search(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ if len(nums) == 0: return -1 if len(nums) == 1: if target == nums[0]: return 0 else: return -1 if target < nums[0]: j = -1 while j >= - len(nums): if target == nums[j]: return len(nums) + j if target < nums[j]: if j - 1 >= - len(nums): if nums[j-1] <= nums[j]: j = j - 1 continue else: return -1 else: return -1 if target > nums[j]: return -1 if target == nums[0]: return 0 if target > nums[0]: i = 0 while i < len(nums): if target == nums[i]: return i if target > nums[i]: if i + 1 < len(nums): if nums[i] <= nums[i+1]: i = i + 1 continue else: return -1 else: return -1 if target < nums[i]: return -1 |
如果您有好的建议,欢迎来信与我交流

也欢迎关注微信公众号“苔原带”

