Re: [閒聊] 每日leetcode已回收

看板Marginalman作者 (史萊哲林的優等生)時間1年前 (2024/05/20 17:25), 編輯推噓3(301)
留言4則, 4人參與, 1年前最新討論串248/1554 (看更多)
※ 引述《Rushia (早瀬ユウカの体操服 )》之銘言: : https://leetcode.com/problems/sum-of-all-subset-xor-totals/description : 1863. Sum of All Subset XOR Totals : 給你一個陣列nums,求出他的所有子集合裡面的元素相互xor之後的和。 : 思路: : 1.回溯法,窮舉所有子集合並在途中計算每個子集合的xor結果加總。 在backtrack裡面呼叫兩個backtrack 一個包含自己,一個不包含自己 Code: impl Solution { pub fn subset_xor_sum(nums: Vec<i32>) -> i32 { fn backtrack(nums: &Vec<i32>, index: usize, current_xor: i32, total: &mut i32) { if index == nums.len() { *total += current_xor; return; } backtrack(nums, index + 1, current_xor, total); backtrack(nums, index + 1, current_xor ^ nums[index], total); } let mut total = 0; backtrack(&nums, 0, 0, &mut total); total } } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 60.248.143.172 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1716197135.A.83F.html

05/20 17:26, 1年前 , 1F
別卷了
05/20 17:26, 1F

05/20 17:26, 1年前 , 2F
別捲了
05/20 17:26, 2F

05/20 17:27, 1年前 , 3F
好討厭數學解 我就這樣躺平了
05/20 17:27, 3F

05/20 17:35, 1年前 , 4F
別捲了
05/20 17:35, 4F
文章代碼(AID): #1cInSFW_ (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1cInSFW_ (Marginalman)