LeetCode(力扣)——Search in Rotated Sorted Array 搜索旋转排序数组 python实现

题目描述:

python实现 Search in Rotated Sorted Array 搜索旋转排序数组  

  中文:假设按照升序排序的数组在预先未知的某个点上进行了旋转。

  ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。

  搜索一个给定的目标值,如果数组中存在这个目标值,则返回它的索引,否则返回 -1 。

  你可以假设数组中不存在重复的元素。

  你的算法时间复杂度必须是 O(log n) 级别。

  英文:Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).

You are given a target value to search. If found in the array return its index, otherwise return -1.

You may assume no duplicate exists in the array.

Your algorithm‘s runtime complexity must be in the order of O(log n).

 1 class Solution(object):
 2     def search(self, nums, target):
 3         """
 4         :type nums: List[int]
 5         :type target: int
 6         :rtype: int
 7         """
 8         start = 0
 9         end = len(nums)-1
10         while start<=end:
11             mid = (start + end)/2   #对于整数会自动省去小数部分
12             if nums[mid] == target:
13                 return mid
14             if nums[mid]>=nums[start]:
15                 if target >= nums[start] and target<=nums[mid]:
16                     end = mid-1
17                 else:
18                     start = mid + 1
19
20             if nums[mid]<nums[end]:
21                 if target > nums[mid] and target<=nums[end]:
22                     start = mid+1
23
24                 else:
25                     end = mid -1
26         return -1

题目来源:力扣题库

原文地址:https://www.cnblogs.com/spp666/p/11536041.html

时间: 2024-10-10 12:03:44

LeetCode(力扣)——Search in Rotated Sorted Array 搜索旋转排序数组 python实现的相关文章

leetcode题解:Search in Rotated Sorted Array(旋转排序数组查找)

题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no du

LeetCode OJ:Search in Rotated Sorted Array(翻转排序数组的查找)

Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplic

[Leetcode] search in rotated sorted array 搜索旋转有序数组

Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplicate

lintcode 容易题:Recover Rotated Sorted Array恢复旋转排序数组

题目: 恢复旋转排序数组 给定一个旋转排序数组,在原地恢复其排序. 样例 [4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5] 挑战 使用O(1)的额外空间和O(n)时间复杂度 说明 什么是旋转数组? 比如,原始数组为[1,2,3,4], 则其旋转数组可以是[1,2,3,4], [2,3,4,1], [3,4,1,2], [4,1,2,3] 解题: 开始我想,先找到中间的临界点,然后在排序,临界点找到了,排序不知道怎么搞了,在这里,看到了很好的方法,前半部分逆序,后半部分逆序,整

[LeetCode] Search in Rotated Sorted Array 在旋转有序数组中搜索

Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no duplic

[CareerCup] 11.3 Search in Rotated Sorted Array 在旋转有序矩阵中搜索

11.3 Given a sorted array of n integers that has been rotated an unknown number of times, write code to find an element in the array. You may assume that the array was originally sorted in increasing order. EXAMPLE Input: find 5 in {15, 16, 19, 20, 2

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

leetcode 题解:Remove Duplicates from Sorted Array(已排序数组去重)

题目: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example,Given input array A

LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)

Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 这个和前面的一个不一样就在于可能会有重复的数字,那么判断的时候就应该注意了,遇到start