class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
int n = nums.size();
if(n < 3){
return false;
}
int first = nums[0], second = INT_MAX;
for(int i = 1; i < n; ++i){
int third = nums[i];
if(third > second){ //如果third比second大,那就是找到了,直接返回true
return true;
}else if(third > first){ //如果third比second小,但是比first大,那就把second指向third,然后继续遍历找third
second = third;
}else if(third < first){ //如果third比first还小,那就把first指向third,然后继续遍历找third(这样的话first会跑到second的后边,但是不要紧,因为在second的前边,老first还是满足的)
first = third;
}
}
return false;
}
};
LeetCode 334. 递增的三元子序列
最新推荐文章于 2024-08-14 11:57:07 发布