文章目录
1. 题目
2. 思路
(1) 贪心算法
- 遍历数组,维护两个变量first和second,first表示三元子序列的第一个数,second表示三元子序列的第二个数。
- 维护first是前i个数的最小数,second是前i个数的次小数,若nums[i]>second,则表示找到第三个数,三元子序列存在。
3. 代码
public class Test {
public static void main(String[] args) {
}
}
class Solution {
public boolean increasingTriplet(int[] nums) {
int first = Integer.MAX_VALUE;
int second = Integer.MAX_VALUE;
for (int num : nums) {
if (num <= first) {
first = num;
} else if (num <= second) {
second = num;
} else {
return true;
}
}
return false;
}
}