(剑指offer)旋转数组的最小数字
来源:互联网 发布:淘宝团队管理制度 编辑:程序博客网 时间:2024/05/16 12:59
题目描述:
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
代码如下:
public int minNumberInRotateArray(int[] array) { if (array == null || array.length == 0) { return 0; } int start = 0; int end = array.length - 1; int middle = start; while (array[start] >= array[end]) { // 如果end和start已经相邻,则middle指向后一个值,并跳出循环 if (end - start == 1) { middle = end; break; } middle = start + (end - start) / 2; // 考虑特殊情况,如旋转后的数组为{1,0,1,1,1} if (array[start] == array[middle] && array[end] == array[middle]) { // 此时,采用顺序查找 return findInOrder(array, start, end); } if (array[middle] >= array[start]) { start = middle; } else if (array[middle] <= array[end]) { end = middle; } } return array[middle]; } private int findInOrder(int[] array, int start, int end) { int result = array[start]; for (int i = 1; i <= end; i++) { if (array[i] <= result) { result = array[i]; } } return result; }
阅读全文
0 0
- 【剑指offer】旋转数组的最小数字
- 剑指offer--旋转数组的最小数字
- 剑指offer--旋转数组的最小数字
- 剑指offer:旋转数组的最小数字
- 《剑指offer》旋转数组的最小数字
- 剑指 offer:旋转数组的最小数字
- 剑指offer-旋转数组的最小数字
- 剑指offer 旋转数组的最小数字
- 【剑指offer】旋转数组的最小数字
- 剑指offer 旋转数组的最小数字
- 剑指offer-旋转数组的最小数字
- 剑指Offer:旋转数组的最小数字
- 剑指Offer 旋转数组的最小数字
- 剑指offer|旋转数组的最小数字
- 剑指offer:旋转数组的最小数字
- 【剑指offer】旋转数组的最小数字
- 《剑指offer》旋转数组的最小数字
- 剑指Offer:旋转数组的最小数字
- request response session
- Ajax的ajax()和post()的使用
- 最大子串和问题
- LeetCode | 80. Remove Duplicates from Sorted Array II
- C++ STL-Vector动态数组
- (剑指offer)旋转数组的最小数字
- #正念奇迹#ZQ0003《搞定:无压工作的艺术》
- java基础知识(数组)
- 布局优化
- matlab+opencv+VS动态编译出错:
- SpringCloud 整合 Quartz 支持集群,支持动态修改 Quartz 执行时间
- HDU
- 关押罪犯
- #正念奇迹#ZQ0004《消除压力,从大脑开始》