Leetcode Note: Go - Sum of All Subnet XOR Totals

Sum of All Subset XOR Totals - LeetCode
https://leetcode.com/problems/sum-of-all-subset-xor-totals/

  • Go 言語で取り組んだメモ

回答

Go backtrack - Sum of All Subset XOR Totals - LeetCode
https://leetcode.com/problems/sum-of-all-subset-xor-totals/solutions/2078954/go-backtrack/

func subsetXORSum(nums []int) int {
    return backtrack(nums, 0)
}

func backtrack(nums []int, curXor int) int {
    if len(nums) == 0 {
        return curXor
    }
    
    withNum := backtrack(nums[1:], curXor ^ nums[0])
    withoutNum := backtrack(nums[1:], curXor)
    
    return withNum + withoutNum
}