本题
Return true if there exists i, j, k
such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return false.
判断是否有三个数满足如上条件,即数值大小顺序和下标大小顺序一致。
我们维护两个常量a,b
a来存储当前最小的数字,b存储最接近a的比a大的数字
首先判断当前值是否小于a 是,赋值给a,否,判断是否小于b ,是 赋值给b,否 说明存在此序列,
返回true。
最后,循环结束 ,返回false;
代码如下:
class Solution {
public boolean increasingTriplet(int[] nums) {
int a = Integer.MAX_VALUE;
int b = Integer.MAX_VALUE;
int n = nums.length;
for (int i = 0; i < n; i++) {
if(nums[i]<=a)
a = nums[i];
else if(nums[i]<=b)
b = nums[i];
else
return true;
}
return false;
}
}