楼主: 
yam276 ('_')   
2024-05-20 17:25:33※ 引述《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
    }
}