[剑指offer]算法6 旋转数组的最小数字
2017-10-28 17:32
218 查看
题目描述
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
时间限制:3秒空间限制:32768K. 这道题目很容易让人把注意力集中到旋转数组上,其实这道题目结果返回的是2最小值,所以我们完全没有不要纠结旋转数组,通过前后两两数字比较得到较小的数字再和第三个数比较,这是C语言简单的算法了。如果对旋转数组感兴趣,可以阅读我的这篇文章里面就是给定一个数组如何得到旋转数组。
链接:http://blog.csdn.net/h2677lucy/article/details/78278524
package Offer; import java.util.ArrayList; class Hell{ public int minNumberInRotateArray(int [] array) { int min=array[0]; for(int i=0;i<array.length;i++) { if(min>=array[i]) { min=array[i]; } } return min; } } public class Test { public static void main(String[] args) { // TODO Auto-generated method stub int[] array = {6501,6828,6963,7036,7422,7674,8146,8468,8704,8717,9170,9359,9719,9895,9896,9913,9962,154,293,334,492,1323,1479,1539,1727,1870,1943,2383,2392,2996,3282,3812,3903,4465,4605,4665,4772,4828,5142,5437,5448,5668,5706,5725,6300,6335}; Hell solution = new Hell(); int op = solution.minNumberInRotateArray(array); System.out.println(op); } }结果为:154
时间限制和内存限制都在范围内。
相关文章推荐
- 剑指offer-算法题练习:part6 旋转数组的最小数字
- 剑指offer---旋转数组中最小的数字(8)
- 剑指offer---旋转数组的最小数字
- [剑指offer][面试题08]求旋转数组的最小数字
- 剑指offer之面试题8:旋转数组的最小数字
- 【剑指Offer面试编程题】题目1386:旋转数组的最小数字--九度OJ
- 剑指Offer:旋转数组中的最小数字
- 剑指offer(旋转数组中的最小的数字)
- 剑指offer_6 旋转数组最小的数字
- 剑指offer8:旋转数组的最小数字
- [牛客网,剑指offer,python] 旋转数组的最小数字
- 牛客网剑指offer-旋转数组的最小数字
- 剑指OFFER之旋转数组的最小数字(九度OJ1386)
- 剑指offer-面试题8:旋转数组中的最小数字
- 剑指offer--6.旋转数组的最小数字
- 剑指Offer:旋转数组的最小数字
- 剑指Offer:旋转数组的最小数字
- 【剑指offer】旋转数组的最小数字
- 剑指offer面试题 旋转数组的最小数字
- 剑指offer面试题8:旋转数组的最小数字