剑指Offer----面试题八:旋转数组的做小数字
来源:互联网 发布:矢量图软件哪个好 编辑:程序博客网 时间:2024/05/22 03:01
题目:
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个递增排序的数组的一个旋转,输出旋转数组的最小元素。例如数组{3,4,5,1,2,}为{1,2,3,4,5}的一个旋转,该数组的最小值为1.
思路一:
按照传统思路,将数组中的值一个个相互比较,取出来做小的数字。
源代码如下:
#include<iostream>using std::cout;using std::endl;int Min1(int *arr, int length){if (arr == NULL || length <= 0)throw std::exception("The array is empty");int temp = arr[0];for (int i = 1; i > length; i++){if (temp < arr[i])temp = arr[i];}return temp;}void test2(){int arr[5] = {3,4,5,1,2};int min = Min1(arr, 5);cout << min << endl;}int main1(){test2();system("pause");return 0;}
1请按任意键继续. . .
分析:思路一的时间复杂度为O(n),且我们没有利用到数组的特性,即数组是部分有序的。
思路二:
旋转之后的数组将数组分为两个部分且前一部分数组元素的值都大于后一部分数组元素的值,两部分都是有序的,进一步观察我们注意到最小元素是两个子数组的分界线。因此我们可以用二分查找的思想。充分利用已知条件,减少时间复杂度。
源代码如下:
#include<iostream>using std::cout;using std::endl;int Min2(int *arr, int length){if (arr == NULL || length <= 0)throw std::exception("The input array is empty");int *p1 = arr;int *p2 = arr + length - 1;while ((p1 + 1) != p2){int *mid = p1 + (p2 - p1) / 2;//处理当中间值在左边递增数组时的情况,一定是大于等于,方便处理数组中元素相同的情况if (*mid >= *p1)p1 = mid;//处理当中间值在右边递增数组时的情况else if (*mid <= *p2)p2 = mid;}return *p2;}void test3(){int arr[] = { 3, 4, 5, 1, 2 };cout << Min2(arr, 5) << endl;}void test4(){int arr[] = { 3, 4, 1, 2 };cout << Min2(arr, 4) << endl;}int main(){test3();test4();system("pause");return 0;}
11请按任意键继续. . .
课本源代码:
int Min(int *numbers, int length){if (numbers == NULL || length <= 0)throw std::exception("Invalid parameters");int index1 = 0;int index2 = length - 1;int indexMid = index1;while (numbers[index1] >= numbers[index2]){if (index2 - index1 == 1){indexMid = index2;break;}indexMid = (index1 + index2) / 2;if (numbers[indexMid] >= numbers[index1])index1 = indexMid;else if (numbers[indexMid] <= numbers[index2])index2 = indexMid;return numbers[indexMid];}}
显然思路一并没有考虑到这种情况!
思路三:
我们无法判断中间的数字是位于前面的子数组中还是后面的子数组中,也就无法移动两个指针来缩小查找的范围,此时我们不得不采用顺序查找的方法!
官方源代码:
// MinNumberInRotatedArray.cpp : Defines the entry point for the console application.//// 《剑指Offer——名企面试官精讲典型编程题》代码// 著作权所有者:何海涛#include<iostream>#include<cstdio>#include<exception>int MinInOrder(int* numbers, int index1, int index2);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;}// ====================测试代码====================void Test(int* numbers, int length, int expected){int result = 0;try{result = Min(numbers, length);for (int i = 0; i < length; ++i)printf("%d ", numbers[i]);if (result == expected)printf("\tpassed\n");elseprintf("\tfailed\n");}catch (...){if (numbers == NULL)printf("Test passed.\n");elseprintf("Test failed.\n");}}int main(){// 典型输入,单调升序的数组的一个旋转int array1[] = { 3, 4, 5, 1, 2 };Test(array1, sizeof(array1) / sizeof(int), 1);// 有重复数字,并且重复的数字刚好的最小的数字int array2[] = { 3, 4, 5, 1, 1, 2 };Test(array2, sizeof(array2) / sizeof(int), 1);// 有重复数字,但重复的数字不是第一个数字和最后一个数字int array3[] = { 3, 4, 5, 1, 2, 2 };Test(array3, sizeof(array3) / sizeof(int), 1);// 有重复的数字,并且重复的数字刚好是第一个数字和最后一个数字int array4[] = { 1, 0, 1, 1, 1 };Test(array4, sizeof(array4) / sizeof(int), 0);// 单调升序数组,旋转0个元素,也就是单调升序数组本身int array5[] = { 1, 2, 3, 4, 5 };Test(array5, sizeof(array5) / sizeof(int), 1);// 数组中只有一个数字int array6[] = { 2 };Test(array6, sizeof(array6) / sizeof(int), 2);// 输入NULLTest(NULL, 0, 0);system("pause");return 0;}
运行结果:
3 4 5 1 2 passed3 4 5 1 1 2 passed3 4 5 1 2 2 passed1 0 1 1 1 passed1 2 3 4 5 passed2 passedTest passed.请按任意键继续. . .
0 0
- 剑指Offer----面试题八:旋转数组的做小数字
- 剑指offer面试题八:旋转数组的最小数字
- 《剑指Offer》面试题八之旋转数组的最小数字
- 剑指offer面八_旋转数组的最小数字
- 【剑指offer】面试题8:旋转数组的最小数字
- 剑指offer面试题8:旋转数组的最小数字
- 剑指Offer:面试题8 :旋转数组的最小数字
- 《剑指offer》面试题8旋转数组的最小数字
- 《剑指Offer》之面试题旋转数组的最小数字
- 《剑指Offer》面试题8:旋转数组的最小数字
- 【剑指offer】 面试题8: 旋转数组的最小数字
- 《剑指Offer》面试题:旋转数组的最小数字
- 旋转数组的最小数字(剑指offer面试题)
- 剑指offer-面试题8:旋转数组的最小数字
- 剑指offer-面试题8.旋转数组的最小数字
- 剑指offer面试题 旋转数组的最小数字
- 剑指offer面试题8:旋转数组的最小数字
- 【剑指offer】面试题8:旋转数组的最小数字
- 端口号
- 考试 —— NOI2016 山东省队选拔第一轮 Day 1
- 关于网页打印软件Lodop
- 字符串相关类型题目及其实现
- Flexbox布局(2)
- 剑指Offer----面试题八:旋转数组的做小数字
- HTTP请求报文和HTTP响应报文
- iOS开发的几个小问题(二)
- flume 简单案例 将一个节点的中一个日志文件的动态变化 在 另一个节点的终端输出
- C++ 高级篇(五)—— 预处理指令
- OC 类方法的调用
- 3.矩阵分析与应用
- Hibernate级联查询实体关系映射
- C++ 标准函数库 —— 文件的输入输出 (Input/Output with files)