Leetcode 334. Increasing Triplet Subsequence (Python)

来源:互联网 发布:儿童汉语拼音拼读软件 编辑:程序博客网 时间:2024/06/07 13:34

题目大意

确定一个未排序数组中是否存在长度为3的上升子序列

算法

和leetcode 300思路一样,大家可以看这里

代码

class Solution(object):    def binary_search(self,num):        start = 0        end = len(self.LIS) - 1        if self.LIS[0] >= num:            return 0        while end-start>1:            middle = (start+end)/2            if self.LIS[middle] > num:                end = middle            elif self.LIS[middle] < num:                start = middle            else:                return middle        return end    def increasingTriplet(self, nums):        """        :type nums: List[int]        :rtype: bool        """        if len(nums) == 0:            return False        self.LIS = [nums[0]]        for i in range(1,len(nums)):            num = nums[i]            if num > self.LIS[-1]:                self.LIS.append(num)            else:                index = self.binary_search(num)                self.LIS[index] = num            if len(self.LIS) > 2:                return True        return False
0 0
原创粉丝点击