zoukankan      html  css  js  c++  java
  • leetcode-mid-array-334 Increasing Triplet Subsequence-NO

    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
  • 相关阅读:
    C语言寒假大作战02
    C语言寒假大作战01
    学习总结
    C语言I作业11
    C语言I作业10
    C语言I博客作业09
    C语言I作业08
    实验五、单元测试
    实验四 代码审查
    UML 建模工具的安装与使用
  • 原文地址:https://www.cnblogs.com/rosyYY/p/10964149.html
Copyright © 2011-2022 走看看