LeetCode75-334-Increasing-Triplet-Subsequence

🧠 思路总结

使用 贪心思想, 找局部最优。使用两个变量first和second记录当前的最小和第二小的数字。(通过遍历数组来找两个变量,如果不更新这两个变量的话就意味着这个数字比这两个变量大,返回true)

❗ 易错点 / 小坑

局部最优更新:

  • 更新 first 时,second 可能比之前的小

  • 更新 second 时,一定要保证它比 first 大

  • 这保证最终找到第三个大于 second 的数时 triplet 成立

✅ 代码(Java)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public boolean increasingTriplet(int[] nums) {
int first = Integer.MAX_VALUE;
int second = Integer.MAX_VALUE;
for (int i = 0; i < nums.length; i++){
if (nums[i] <= first){
first = nums[i]; // Smallest number
} else if (nums[i] <= second){
second = nums[i]; // The Second smallest number
} else {
return true;
}
}
return false;
}
}

LeetCode75-334-Increasing-Triplet-Subsequence
https://powersixxx.github.io/leetcode-blog/2026/01/31/LeetCode75-334-Increasing-Triplet-Subsequence/
作者
Baowen Liu
发布于
2026年1月31日
许可协议