【剑指offer】求旋转数组的最小数字8
来源:互联网 发布:ubuntu sys videoio.h 编辑:程序博客网 时间:2024/05/07 07:45
#include<iostream>#include <stdio.h>//求旋转数组的最小数字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;}int Min(int * numbers,int length){if(numbers == NULL || length <= 0)throw new std::exception("Invalid parameters");int index1=0;int index2=length-1;//如果排序数组的前面的0个元素搬到后面,即排序数组本身仍然是一个旋转。那么数组的第一个数组//为最小的数组,可以直接返回,所以把indexMid设为index1int indexMid=index1;while(numbers[index1] >= numbers[index2]){// 如果index1和index2指向相邻的两个数, // 则index1指向第一个递增子数组的最后一个数字, // index2指向第二个子数组的第一个数字,也就是数组中的最小数字if(index2-index1==1){indexMid=index2;break;}indexMid=(index1+index2)/2;// 如果下标为index1、index2和indexMid指向的三个数字相等, // 则只能顺序查找 此处不明白可查看书上内容if(numbers[index1]==numbers[index2] && numbers[index1] == numbers[indexMid])return MinInOrder(numbers,index1,index2);if(numbers[indexMid] >= numbers[index1])index1=indexMid;else if(numbers[indexMid] <= numbers[index2])index2=indexMid;}return numbers[indexMid];}// ====================测试代码====================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"); else printf("\tfailed\n"); } catch (...) { if(numbers == NULL) printf("Test passed.\n"); else printf("Test failed.\n"); }}void 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); // 输入NULL Test(NULL, 0, 0);}
0 0
- 【剑指offer】求旋转数组的最小数字8
- 【剑指offer】旋转数组的最小数字
- 剑指offer--旋转数组的最小数字
- 剑指offer--旋转数组的最小数字
- 剑指offer:旋转数组的最小数字
- 《剑指offer》旋转数组的最小数字
- 剑指 offer:旋转数组的最小数字
- 剑指offer-旋转数组的最小数字
- 剑指offer 旋转数组的最小数字
- 【剑指offer】旋转数组的最小数字
- 剑指offer 旋转数组的最小数字
- 剑指offer-旋转数组的最小数字
- 剑指Offer:旋转数组的最小数字
- 剑指Offer 旋转数组的最小数字
- 剑指offer|旋转数组的最小数字
- 剑指offer:旋转数组的最小数字
- 【剑指offer】旋转数组的最小数字
- 《剑指offer》旋转数组的最小数字
- Combinations
- 高效能程序员的七个习惯
- 帝国的重生
- hdu 2113 被虐!!
- ListView 加载数据
- 【剑指offer】求旋转数组的最小数字8
- 用GLUT库开始玩儿OpenGL
- [翻译]继承结构的进化
- ASP.NET调用JavaScript
- std' : is not a class or namespace name,解决
- 使用Commons Chain(2)
- JAVA游戏入门开发DAY 2 基本架构
- 全面深入认识C变量
- 启动无窗口VirtualBox虚拟机 以便用远程桌面连接