The easiest method:
三层嵌套结构:
from typing import List
class Solution:
def increasingTriplet(self, nums: List[int]) -> bool:
if len(nums) < 3:
return False
i = 0
while i < len(nums)-2:
j=1 + 1
while j < len(nums)-1:
k= j + 1
while k < len(nums):
if nums[i] < nums[j] and nums[j] < nums[k]:
return i,j,k
k += 1
j+=1
i+=1
return False
solution = Solution()
solution.increasingTriplet([0,4,2,1,0,-1,-3])
Linear time complexity:
The most important point is that the second minimum number indicates there is a smaller number before it, we only need to find other number larger than this number.
A number larger than second number implies there are two smaller number in front.
A number larger than third number means there is three smaller number in front.
class Solution:
def increasingTriplet(self, nums: List[int]) -> bool:
if len(nums)<3:
return False
min_number = float('inf')
second_number = float('inf')
for num in nums:
if num <= min_number:
min_number = num
elif num <= second_number:
second_number = num
else:
return True
return False