旋转数组中的最小数字

题目描述:把一个数组最开始的若干个元素移动到数组的末尾,称之为一个数组的旋转。输入一个递增排序的数组的旋转,输出旋转数组的最小元素。

例如:数组 {3,4,5,1,2} 为{1,2,3,4,5} 的一个旋转,该数组的最小元素为 1。

分析:

int Min(int* numbers, int length)
{
    if(numbers == NULL || length <= 0)
        throw new std::exception("Invalid parameters");
 
    int index1 = 0;
    int index2 = length - 1;
    int indexMid = index1;
    while(numbers[index1] >= numbers[index2])
    {
        // 如果index1和index2指向相邻的两个数,
        // 则index1指向第一个递增子数组的最后一个数字,
        // index2指向第二个子数组的第一个数字,也就是数组中的最小数字
        if(index2 - index1 == 1)
        {
            indexMid = index2;
            break;
        }
 
        // 如果下标为index1、index2和indexMid指向的三个数字相等,
        // 则只能顺序查找
        
        indexMid = (index1 + index2) / 2;
        // 缩小查找范围
        if(numbers[indexMid] >= numbers[index1])
            index1 = indexMid;
        else if(numbers[indexMid] <= numbers[index2])
            index2 = indexMid;
    }
 
    return numbers[indexMid];
}

int Min(int* numbers, int length)
{
    if(numbers == NULL || length <= 0)
        throw new std::exception("Invalid parameters");
 
    int index1 = 0;
    int index2 = length - 1;
    int indexMid = index1;
    while(numbers[index1] >= numbers[index2])
    {
        // 如果index1和index2指向相邻的两个数,
        // 则index1指向第一个递增子数组的最后一个数字,
        // index2指向第二个子数组的第一个数字,也就是数组中的最小数字
        if(index2 - index1 == 1)
        {
            indexMid = index2;
            break;
        }
 
        // 如果下标为index1、index2和indexMid指向的三个数字相等,
        // 则只能顺序查找
        indexMid = (index1 + index2) / 2;
        if(numbers[index1] == numbers[index2] && numbers[indexMid] == numbers[index1])
            return MinInOrder(numbers, index1, index2);

        // 缩小查找范围
        if(numbers[indexMid] >= numbers[index1])
            index1 = indexMid;
        else if(numbers[indexMid] <= numbers[index2])
            index2 = indexMid;
    }
 
    return numbers[indexMid];
}

int MinInOrder(int* numbers, int index1, int index2)
{
    int result = numbers[index1];
    for(int i = index1 + 1; i <= index2; ++i)
    {
        if(result > numbers[i])
            result = numbers[i];
    }

    return result;
}

时间: 2024-08-26 18:19:30

旋转数组中的最小数字的相关文章

【LeetCode-面试算法经典-Java实现】【153-Find Minimum in Rotated Sorted Array(找旋转数组中的最小数字)】

[153-Find Minimum in Rotated Sorted Array(找旋转数组中的最小数字)] [LeetCode-面试算法经典-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 m

【LeetCode-面试算法经典-Java实现】【155-Find Minimum in Rotated Sorted Array II(找旋转数组中的最小数字II)】

[154-Find Minimum in Rotated Sorted Array II(找旋转数组中的最小数字II)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Supp

剑指Offer面试题:6.旋转数组中的最小数字

一 题目:旋转数组中的最小数字 题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1. 这道题最直观的解法并不难,从头到尾遍历数组一次,我们就能找出最小的元素.这种思路的时间复杂度显然是O(n).但是这个思路没有利用输入的旋转数组的特性,肯定达不到面试官的要求. 我们注意到旋转之后的数组实际上可以划分为两个排序的子数组,而且前面的子数组

【剑指offer】六,旋转数组中的最小数字

题目描述 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1. 分析:数组旋转后会变为一个局部有序的数组,分为两部分,都是递增的.查找最小元素,我们首先想到的时冒泡排序一遍,或者一趟选择排序.或者直接找出a[i]<a[i-1]的点即为要查找的点.也可利用折半查找,判断最小值所在的一侧. 1 import java.util.ArrayLis

剑指Offer之旋转数组中的最小数字(题8)

1 /****************************************   2     > File Name:test.c   3     > Author:xiaoxiaohui   4     > mail:[email protected]   5     > Created Time:2016年05月23日 星期一 20时07分13秒   6 ****************************************/   7    8    9 

旋转数组中的最小数字,剑指offer,P70 二分查找来实现O(logn)的查找

public class MinNumberInRotatedArray { public int getMinNumInRotatedArray(int[] array) { if(array == null) { return -1; } int leftIndex = 0; int rightIndex = array.length - 1; if(array[leftIndex] == array[rightIndex]) { return array[0]; } int midInde

【c语言】输入一个递增排序的数组的一个旋转,输出旋转数组中的最小元素

//旋转数组的最小数字 //题目:把一个数组最開始的若干个元素搬到数组的末尾.我们称之为数组的旋转. //输入一个递增排序的数组的一个旋转.输出旋转数组中的最小元素. //比如:数组{3.4,5,1,2}为{1,2.3.4.5}的一个旋转,最小元素是1. #include <stdio.h> #include <assert.h> int min_equ(int *src, int left, int right) { int i = 0; int ret = src[left];

【编程题目】旋转数组中的最小元素☆

69.旋转数组中的最小元素(数组.算法).题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个排好序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3, 4, 5, 1, 2}为{1, 2, 3, 4, 5}的一个旋转,该数组的最小值为 1. 我就用了最简单的方法.而且开始还没考虑1, 0, 1 ,1这样的情况 /* 69.旋转数组中的最小元素(数组.算法). 题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个 排好序的数组的一个旋转

微软算法100题69 求旋转数组中的最小元素

69.求旋转数组中的最小元素.题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转.输入一个排好序的数组的一个旋转,输出旋转数组的最小元素.例如数组{3, 4, 5, 1, 2}为{1, 2, 3, 4, 5}的一个旋转,该数组的最小值为1 思路: 1. 可以从左到右扫描整个数组,找出最小的那个,时间复杂度是o(n) 2. 也可以利用旋转数组本身的特性,即部分有序,然后借助二分查找的方法来解决,先找中间点mid = left + (right - left)/2, 如果a[le