[LeetCode] 153. Find Minimum in Rotated Sorted Array_Medium
发布时间:2020-12-14 03:47:36 所属栏目:大数据 来源:网络整理
导读: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,7,2] ). Find the minimum element. You may assume no duplicate exists in the array. Example 1: Input:
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e.,? Find the minimum element. You may assume no duplicate exists in the array. Example 1: Input: [3,2] Output: 1 Example 2: Input: [4,2] Output: 0 ? 这个题目思路就是找到first number s.t <= nums[-1].利用Binary Search T: O(lgn) Code class Solution: def minRotatedArray(self,nums): l,r,target = 0,len(nums) -1,nums[-1] if not nums: return -1 while l + 1 < r: mid = l + (r - l)//2 if nums[mid] <= target: r = mid else: l = mid return min(nums[l],nums[r]) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |