题目:输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。
方法1:用python自带的方法sort,此时list本身将被修改,且 list.sort() 返回的类型是nonetype
# -*- coding:utf-8 -*-
class Solution:
def minNumberInRotateArray(self, rotateArray):
# write code here
if rotateArray is None:
return 0
else:
rotateArray.sort()
return rotateArray[0]
方法2:调用sorted()方法。它返回一个新的list sorted(list)
# -*- coding:utf-8 -*-
class Solution:
def minNumberInRotateArray(self, rotateArray):
# write code here
if rotateArray is None:
return 0
else:
li = sorted(rotateArray)
return li[0]