334. incremental ternary sequence
Given an unsorted array, the array is determined whether or not there is increasing subsequence length of 3.
Mathematical expressions are as follows:
If there is such i, j, k, and satisfies 0 ≤ i <j <k ≤ n-1,
so that arr [i] <arr [j ] <arr [k], returns true; otherwise returns false.
Description: The time requirements of the algorithm complexity is O (n), the spatial complexity is O (1).
Example 1:
Input: [1,2,3,4,5]
Output: true
Example 2:
Input: [5,4,3,2,1]
Output: false
class Solution {
public boolean increasingTriplet(int[] nums) {
if (nums==null||nums.length<3) return false;
int big=Integer.MAX_VALUE,small=Integer.MAX_VALUE;
for (int i:nums){
// 通过if的结构保证递增!
if (i<=small) small=i;
// 走到这一步说明这个值大于前面的值(i>small)
else if (i<=big) big=i;
// 走到这一步说明这个值大于前面的两个值(i>big>small)
else return true;
}
return false;
}
}