LeetCode 334 Increasing Triplet Subsequence (Python)

Posted by 小明MaxMing on December 18, 2020

题目

Given an integer array nums, return true if there exists a triple of indices (i, j, k) such that i < j < k and nums[i] < nums[j] < nums[k]. If no such indices exists, return false.

解题思路

使用两个指针,一个记录最小的,一个记录次小的,如果再出现一个比两者都大的,则存在

代码

class Solution:
    def increasingTriplet(self, nums: List[int]) -> bool:
        first = second = float('inf')
        for n in nums:
            if n <= first:
                first = n
            elif n <= second:
                second = n
            else:
                return True
        return False

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅