153. Find Minimum in Rotated Sorted Array 寻找旋转排序数组中的最小值
【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)
Section titled “【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)”标签: LeetCode
题目地址:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/
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).
Find the minimum element.
You may assume no duplicate exists in the array.
找出旋转有序数组中的最小值。
这个题是剑指offer上的原题,这里在复习一下。看到有序的数组就想到二分查找。这个是变种而已。
注意边界和循环条件。
class Solution(object): def findMin(self, nums): """ :type nums: List[int] :rtype: int """ if len(nums) == 1: return nums[0] left, right = 0, len(nums) - 1 mid = left while nums[left] >= nums[right]: if left + 1 == right: mid = right break mid = (left + right) / 2 if nums[mid] >= nums[left]: left = mid elif nums[mid] <= nums[right]: right = mid return nums[mid]2018 年 3 月 12 日

评论与交流