Re: [閒聊] 每日leetcode

看板Marginalman作者 (溫水佳樹的兄長大人)時間7月前 (2025/04/27 18:44), 編輯推噓0(006)
留言6則, 2人參與, 7月前最新討論串1408/1548 (看更多)
※ 引述《yam276 (史萊哲林的優等生)》之銘言: : 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 : } : } 思路: 照題目敘述做題 一路滑到底就是答案 Python Code: class Solution: def countSubarrays(self, nums: List[int]) -> int: result = 0 for i in range(len(nums)-2): if nums[i] + nums[i+2] == nums[i+1] / 2: result += 1 return result -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.194.160.111 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1745750668.A.79B.html

04/27 18:50, 7月前 , 1F
不用考慮中間數是偶數嗎
04/27 18:50, 1F

04/27 18:53, 7月前 , 2F
奇數除出來就浮點數 一定不等於所以沒差ㄅ
04/27 18:53, 2F

04/27 18:54, 7月前 , 3F
哀 只有PY可以這樣玩了
04/27 18:54, 3F

04/27 18:54, 7月前 , 4F
恨動態語言
04/27 18:54, 4F

04/27 18:55, 7月前 , 5F
py就是這點方便
04/27 18:55, 5F

04/27 18:55, 7月前 , 6F
不過工作沒寫好就會噴怪東西 哀
04/27 18:55, 6F
文章代碼(AID): #1e3WgCUR (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1e3WgCUR (Marginalman)