码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode:递增的三元子序列【334】

时间:2018-08-19 01:03:07      阅读:141      评论:0      收藏:0      [点我收藏+]

标签:for   cas   .com   als   info   题解   pre   空间   bubuko   

LeetCode:递增的三元子序列【334】

题目描述

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

数学表达式如下:

如果存在这样的 i, j, k,  且满足 0 ≤ i < j < k ≤ n-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

题目分析

技术分享图片

Java题解

class Solution {
    public boolean increasingTriplet(int[] nums) {
        if(nums.length<3)
            return false;
        int min1 = Integer.MAX_VALUE;
        int min2 = Integer.MAX_VALUE;
        for(int i=0;i<nums.length;i++)
        {
            //CASE3
            if(nums[i]>min2)
                return true;
            if(nums[i]<min1)
                min1=nums[i];
            if(nums[i]>min1&&nums[i]<min2)
                min2=nums[i];
        }
        return false;
    }
}

 

LeetCode:递增的三元子序列【334】

标签:for   cas   .com   als   info   题解   pre   空间   bubuko   

原文地址:https://www.cnblogs.com/MrSaver/p/9499233.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!