Re: [閒聊] 每日leetcode

看板Marginalman作者 (史萊哲林的優等生)時間7月前 (2025/04/27 18:41), 7月前編輯推噓0(000)
留言0則, 0人參與, 最新討論串1407/1548 (看更多)
3392. Count Subarrays of Length Three With a Condition https://leetcode.com/problems/count-subarrays-of-length-three-with-a-condition/ 簡單來說 任意三個數的子陣列切片 中間的數 要是兩邊相加除二 思考: Sliding Windows 而且 Rust 居然有內建 .widnows() 函數 沒內建的語言用 for i in 0..nums.len()-2 { let a = nums[i]; let b = nums[i+1]; let c = nums[i+2]; // ... } Code: impl Solution { pub fn count_subarrays(nums: Vec<i32>) -> i32 { nums.windows(3) .filter(|w| w[1] % 2 == 0 && w[1] / 2 == w[0] + w[2]) .count() as i32 } } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.193.249.242 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1745750494.A.02B.html ※ 編輯: yam276 (123.193.249.242 臺灣), 04/27/2025 18:42:22
文章代碼(AID): #1e3WdU0h (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1e3WdU0h (Marginalman)