具体思路:
赋初始值的时候,已经满足second > first了,现在找第三个数third
(1) 如果third比second大,那就是找到了,直接返回true
(2) 如果third比second小,但是比first大,那就把second的值设为third,然后继续遍历找third
(3) 如果third比first还小,那就把first的值设为third,然后继续遍历找third(这样的话first会跑到second的后边,但是不要紧,因为在second的前边,老first还是满足的)
具体代码:
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
if(nums.size()<3)
return false;
int first=nums[0];
int second=INT_MAX;
for(int i=1;i<nums.size();i++){
if(nums[i]>second)
return true;
if(nums[i]>first&&nums[i]<second){
second=nums[i];
continue;
}
if(nums[i]<first)
first=nums[i];
}
return false;
}
};
/*
2 1 5 0 4 6
2 -1 4 -5 4 2
*/