码迷,mamicode.com
首页 > 编程语言 > 详细

剑指06旋转数组的最小数字

时间:2020-07-22 11:41:13      阅读:59      评论:0      收藏:0      [点我收藏+]

标签:div   大小   res   public   write   min   ret   元素   最小数   

题目描述

把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。
输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。
例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
class Solution {
public:
    int minNumberInRotateArray(vector<int> rotateArray) {
        int  size=rotateArray.size();
        if (size==0)
        {
            return 0;
        }
        int left=0,right=size-1;
        int mid=0;
        while (rotateArray[left]>=rotateArray[right]){
            if (right-left==1){
                mid=right;
                break;
            }
            mid=left+(right-left)/2;
            if (rotateArray[left]==rotateArray[right]&& rotateArray[left]==rotateArray[mid]){
                return  MinOrder(rotateArray,left,right);
            }
            if (rotateArray[mid]>=rotateArray[left]){
                left=mid;
            }
            else {
                right=mid;
            }
        }
        return rotateArray[mid];
    }
    
private :
    int MinOrder(vector <int > &num,int left,int right){
        int result=num[left];
        for (int i=left+1;i<right;++i){
            if (num[i]<result)
            {
                result=num[i];
            }
        }
        return result;
    }
};
import java.util.ArrayList;
public class Solution {
    public int minNumberInRotateArray(int [] array) {
    
        int low=0;int high=array.length-1;
        while (low<high){
            int mid=low+(high-low)/2;
            if (array[mid]>array[high]){
                low=mid+1;
            }else if (array[mid]==array[high]){
                high=high-1;
            }else {
                high=mid;
            }
        }
        return array[low];
    }
}
# -*- coding:utf-8 -*-
class Solution:
    def minNumberInRotateArray(self, rotateArray):
        # write code here
        if not rotateArray:
            return 0
        else :
            return min(rotateArray)
 
 
 
 
 
 
 

剑指06旋转数组的最小数字

标签:div   大小   res   public   write   min   ret   元素   最小数   

原文地址:https://www.cnblogs.com/hrnn/p/13358899.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!