Re: [閒聊] 每日leetcode

看板Marginalman作者 (神楽めあ的錢包)時間2小時前 (2025/12/09 23:35), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1552/1552 (看更多)
3583. Count Special Triplets 用兩個array 一個rec紀錄每個數字出現的次數 另外一個cnt每當x出現時cnt[x]就加上rec[2*x] 這樣你每次答案都只要加上cnt[nums[i]/2]就好 golang : func specialTriplets(nums []int) int { ans, mod, n := 0, 1_000_000_007, len(nums) rec := [200_002]int{} cnt := [200_002]int{} for i := 0; i < n; i++ { a, b := nums[i]/2, nums[i]*2 if nums[i]&1 == 0 && rec[a] > 0 { ans = (cnt[a] + ans) % mod } cnt[nums[i]] = (cnt[nums[i]] + rec[b]) % mod rec[nums[i]] = (rec[nums[i]] + 1) % mod } return ans } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 203.121.235.241 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1765294510.A.1FB.html
文章代碼(AID): #1fE46k7x (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1fE46k7x (Marginalman)