作者:
yam276 ('_')
2025-06-05 18:08:07334. 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;
}
}