精華區beta Marginalman 關於我們 聯絡資訊
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; } } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.32.48.170 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1749118089.A.699.html ※ 編輯: yam276 (114.32.48.170 臺灣), 06/05/2025 18:09:36