【LeetCode-334】Increasing Triplet Subsequence

来源:互联网 发布:淘宝售后客服热线 编辑:程序博客网 时间:2024/05/29 13:50

把easy的刷完了之后,一步一步越来越难了

public class IncreasingTripletSubsequence {/** * 遍历数组,维护一个最小值,和倒数第二小值,遍历原数组的时候, * 如果当前数字小于等于最小值,更新最小值,如果小于等于倒数第二小值,更新倒数第二小值, * 如果当前数字比最小值和倒数第二小值都大,说明此时有三个递增的子序列了,直接返回true,否则遍历结束返回false */public boolean increasingTriplet(int[] nums) {if(nums == null || nums.length < 3){return false;}int min = Integer.MAX_VALUE;int secondMin = Integer.MAX_VALUE;for(int i = 0;i < nums.length;i ++){if(nums[i] < min){min = nums[i];}else if(nums[i] < secondMin){min = nums[i];}else{return true;}}return false;    }}


0 0
原创粉丝点击