[LeetCode]中计算法-递增的三元子序列

 递增的三元子序列

给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列。

数学表达式如下:

如果存在这样的  i, j, k,  且满足 0 ≤ i < j < kn-1,
使得  arr[i] < arr[j] < arr[k] ,返回 true ; 否则返回 false 。

说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1) 。

示例 1:

输入: [1,2,3,4,5]
输出: true

示例 2:

输入: [5,4,3,2,1]
输出: false
class Solution {
    public boolean increasingTriplet(int[] nums) {
        int first=Integer.MAX_VALUE;
        int second=Integer.MAX_VALUE;

        for (int num:nums) {
            if(first>num){
                first=num;
            }else if(first<num&&second>num){
                second=num;
            }else if(num>second){
                return true;
            }
        }
        
        return false;

    }
}

猜你喜欢

转载自blog.csdn.net/AntioniaMao/article/details/81480481