Leetcode做题日记:33. 搜索旋转排序数组(PYTHON)
2019-01-01 15:52
369 查看
假设按照升序排序的数组在预先未知的某个点上进行了旋转。
( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
搜索一个给定的目标值,如果数组中存在这个目标值,则返回它的索引,否则返回 -1 。
你可以假设数组中不存在重复的元素。
你的算法时间复杂度必须是 O(log n) 级别。
示例 1:
输入: nums = [4,5,6,7,0,1,2], target = 0
输出: 4
示例 2:
输入: nums = [4,5,6,7,0,1,2], target = 3
输出: -1
遍历就出答案了
if target not in nums: return -1 i=0 while nums[i]!=target: i=i+1 return i
感觉不符合中等题,试着改一下,时间是一样的,代码倒是变多了
if target not in nums: return -1 i=0 if target>=nums[0]: while target!=nums[i]: i=i+1 return i if target<=nums[-1]: while target!=nums[-1-i]: i=i+1 return len(nums)-i-1
相关文章推荐
- Leetcode 33. 搜索旋转排序数组
- leetcode 33. 搜索旋转排序数组
- Leetcode做题日记:34. 在排序数组中查找元素的第一个和最后一个位置(PYTHON)
- Leetcode 81 搜索旋转排序数组 II Python C++ 史上最详细题解系列
- 搜索旋转排序数组(LeetCode)
- LeetCode 153. 寻找旋转排序数组中的最小值 Python
- 搜索旋转排序数组 - LeetCode
- leetcode33_搜索旋转排序数组
- 33. 搜索旋转排序数组Leetcode
- LeetCode 33 Search in Rotated Sorted Array(在旋转排序数组中搜索)(*)
- LeetCode——旋转排序数组搜索
- 【LeetCode-面试算法经典-Java实现】【081-Search in Rotated Sorted Array II(搜索旋转的排序数组)】
- 【leetcode】有重复的旋转排序数组查找(二分)
- leetCode 33.Search in Rotated Sorted Array(排序旋转数组的查找) 解题思路和方法
- 搜索旋转排序数组
- Leetcode做题日记:37. 解数独(PYTHON)
- LintCode : 搜索旋转排序数组
- Leetcode做题日记:49. 字母异位词分组(PYTHON)
- Leetcode做题日记:52. N皇后 II(PYTHON)
- *[Lintcode]搜索旋转排序数组