leetcode334.递增的三元子序列

1 题目描述

给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列。
数学表达式如下:
如果存在这样的 i, j, k, 且满足 0 ≤ i < j < k ≤ n-1,
使得 arr[i] < arr[j] < arr[k] ,返回 true ; 否则返回 false 。
说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1) 。

2 代码

from typingimport List

class Solution:
  def increasingTriplet(self, nums: List[int]) ->bool:
    if len(nums) <3:
      return False
    big =''
    small = nums[0]
    for i in nums:
      if i <= small:
        small = i
      elif bigis '' or i <= big:
        big = i
      else:
        return True
      return False

if __name__ =="__main__":
  s = Solution()
  print(s.increasingTriplet([2, 1, 5, 0, 3]))

你可能感兴趣的:(leetcode334.递增的三元子序列)