mycode time limited
class Solution(object): def increasingTriplet(self, nums): """ :type nums: List[int] :rtype: bool """ length = len(nums) temp = [] for i,num_i in enumerate(nums[:length-2]): for j,num_j in enumerate(nums[i+1:length-1]): if num_i < num_j: for k in nums[i+j+2:]: if k > nums[i+j+1]: return True return False
参考:
思路:不断更新最大值和最小值,如果elif遇见介于其中的,就可以返回True啦
注意:Python中可以用如下方式表示正负无穷:
float("inf"), float("-inf")
class Solution(object): def increasingTriplet(self, nums): """ :type nums: List[int] :rtype: bool """ min1 = min2 = float('inf') for item in nums: if item <= min1: #[1,1,-2,6] 如果只是<,那么min1=1,min2=1,错误 min1= item elif item < min2: min2 = item elif item > min2: #不是else,否则会出现item=min2的情况 return True return False