,空间复杂度为
表示只能遍历一次
first
< second
< third
,直接返回,否则看 third 落在哪个区间first
< third
,则对 second 进行赋值为 third,这样后续遍历 third > second
就会满足条件first
> third
,则将 first 进行赋值为 third,因为在数组中肯定有比 second 小的数class Solution {
public boolean increasingTriplet(int[] nums) {
if (nums.length < 3) {
return false;
}
int first = nums[0];
int second = Integer.MAX_VALUE;
for (int i = 1; i < nums.length; i++) {
int third = nums[i];
if (third > second) {
return true;
}
if (first < third) {
second = third;
} else {
first = third;
}
}
return false;
}
}
- <End /> -