0%

334. Increasing Triplet Subsequence

O(n)
把这道题转换成类似找第一小第二小第三小的数,最后的a1和a2不一定是最后结果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
int a1 = INT_MAX, a2 = INT_MAX;
for (int x : nums) {
if (x <= a1) {
a1 = x;
} else if (x <= a2) {
a2 = x;
} else {
return true;
}
}
return false;
}
};