[闲聊] 每日leetcode 75 - Day5 - 2

楼主: yam276 ('_')   2025-06-05 18:08:07
334. Increasing Triplet Subsequence
https://leetcode.com/problems/increasing-triplet-subsequence/
题意:
找一个字串是否有 i < j < k 同时 nums[i] < nums[j] < nums[k] 的一组数字存在
思路:
用两个变量哭弱
利用 else if 的特性
当一个数字比 first 大 他就是 second 反之更新first
当一个数字比 second 大 他就是 third 反之更新second
当我们找到 third 就是答案 直接 return true
这种方法像是三重过滤网 因此不用考虑 index 问题
Code:
impl Solution {
pub fn increasing_triplet(nums: Vec<i32>) -> bool {
let mut first = i32::MAX;
let mut second = i32::MAX;
for num in nums {
if num <= first {
first = num;
} else if num <= second {
second = num;
} else {
return true;
}
}
return false;
}
}

Links booklink

Contact Us: admin [ a t ] ucptt.com