题目描述
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。
输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。
例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。
NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。
例如数组{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;
}
};
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];
}
}
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)
class Solution:
def minNumberInRotateArray(self, rotateArray):
# write code here
if not rotateArray:
return 0
else :
return min(rotateArray)