[LeetCode]题解(python):153-Find Minimum in Rotated Sorted Array

题目来源:

  https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/



题意分析:

  在一个不重复的翻转的数组里面找到最小那个。例如:4 5 6 7 0 1 2,最小是0.



题目思路:

  这里可以利用二分的方法去找最小的值。



代码(python):

class Solution(object):
    def findMin(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        size = len(nums)
        if size == 1:
            return nums[0]
        if size == 2:
            return min(nums[0],nums[1])
        mid = size // 2
        if nums[mid - 1] > nums[mid]:
            return nums[mid]
        if nums[mid] < nums[-1]:
            return self.findMin(nums[:mid + 1])
        return self.findMin(nums[mid:])

时间: 2024-10-15 12:27:08

[LeetCode]题解(python):153-Find Minimum in Rotated Sorted Array的相关文章

153. Find Minimum in Rotated Sorted Array - LeetCode

Question 153.?Find Minimum in Rotated Sorted Array Solution 题目大意:给一个按增序排列的数组,其中有一段错位了[1,2,3,4,5,6]变成[4,5,6,1,2,3],把1求出来 思路:遍历,如果当前元素比前一个元素小就是这个元素了 Java实现: public int findMin(int[] nums) { int ans = nums[0]; for (int i=0; i<nums.length; i++) { int pre

Java for LeetCode 153 Find Minimum 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). Find the minimum element. You may assume no duplicate exists in the array. 解题思路: 本题和Java for LeetCode 033 Search in Rotated S

leetcode 154/153. Find Minimum in Rotated Sorted Array &amp;&amp; II

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). Find the minimum element. You may assume no duplicate exists in the array. 分析: 对不包含重复元素的数组A[0, ... , n - 1], 如果A[mid] < A[hig

leetcode 153. Find Minimum in Rotated Sorted Array --------- java

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). Find the minimum element. You may assume no duplicate exists in the array. 在一个反转了的排好序的数组中,找出最小的数 可以直接寻找. public class Solutio

153. Find Minimum 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). Find the minimum element. You may assume no duplicate exists in the array. 题目的意思是想在头尾连在一起是一个排好序的数组里面找出最小的那个数 感觉很简单,于是有了下面的解法

[leedcode 153] Find Minimum 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). Find the minimum element. You may assume no duplicate exists in the array. public class Solution { public int findMin(int[] n

153. Find Minimum in Rotated Sorted Array java solutions

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). Find the minimum element. You may assume no duplicate exists in the array. Subscribe to see which companies asked this questi

LeetCode 153. Find Minimum in Rotated Sorted Array

二分查找. 因为在旋转前的数组是排好序了的, 所以当num[begin] > num[mid]时,表示我们要搜寻的最小数字在num[begin, ..., mid]之间: 反之,num[begin] < num[mid]时,表示我们要搜寻的最小数字在num[mid+1, ..., end]之间(没有被打乱的数组,如1,2,3,4,..,n这种情况除外,在下面代码中我们进行了特判). 例:考虑num = {5, 6, 7, 1, 2, 3, 4}, begin = 0, end = 6, mid

LC.153.Find Minimum in Rotated Sorted Array

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.

153. Find Minimum in Rotated Sorted Array (Array; Divide-and-Conquer)

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. class Solution { public: int f