题目描述:
题解一:直接搜索最小值
其实题目要求就是找到输入数组numbers的最小数字,其中numbers数组分为两段增序排列,最小值为第二段增序的第一个元素。
class Solution(object): def minArray(self, numbers): minnum = numbers[0] for i in range(1,len(numbers)): if numbers[i]<minnum: minnum=numbers[i] return minnum
尝试java版:
class Solution {
public int minArray(int[] numbers) {
int minnum = numbers[0];
for(int i =0;i<numbers.length;i++){
if(numbers[i]<minnum){
minnum = numbers[i];
}
}
return minnum;
}
}
题解二:
从numbers末尾开始搜索,找到一个numbers[i]<numbers[i+1] numbers[i-1]>number[i]的numbers[i]即为最小值。
class Solution(object): def minArray(self, numbers): if len(numbers)==1: return numbers[0] for j in range(len(numbers)-1,0,-1): minnum = numbers[j] if numbers[j-1]>minnum: return minnum if j==1: return numbers[0]