LeetCode每日一题(2425. Bitwise XOR of All Pairings)

You are given two 0-indexed arrays, nums1 and nums2, consisting of non-negative integers. There exists another array, nums3, which contains the bitwise XOR of all pairings of integers between nums1 and nums2 (every integer in nums1 is paired with every integer in nums2 exactly once).

Return the bitwise XOR of all integers in nums3.

Example 1:

Input: nums1 = [2,1,3], nums2 = [10,2,5,0]
Output: 13

Explanation:
A possible nums3 array is [8,0,7,2,11,3,4,1,9,1,6,3].
The bitwise XOR of all these numbers is 13, so we return 13.

Example 2:

Input: nums1 = [1,2], nums2 = [3,4]
Output: 0

Explanation:
All possible pairs of bitwise XORs are nums1[0] ^ nums2[0], nums1[0] ^ nums2[1], nums1[1] ^ nums2[0],
and nums1[1] ^ nums2[1].
Thus, one possible nums3 array is [2,5,1,6].
2 ^ 5 ^ 1 ^ 6 = 0, so we return 0.

Constraints:

  • 1 <= nums1.length, nums2.length <= 105
  • 0 <= nums1[i], nums2[j] <= 109

假设 nums1 = [a, b], nums2 = [c, d, e], (a ^ c) ^ (a ^ d) ^ (a ^ e) = (a ^ a ^ a) ^ (c ^ d ^ e), 因为 a ^ a ^ a = a, 所以上式还可以变化为 a ^ (c ^ d ^ e), 同理,当遍历到 nums1 中的 b 时,就变成了 b ^ (c ^ d ^ e), ans = (a ^ (c ^ d ^ e)) ^ (b ^ (c ^ d ^ e)) = (a ^ b) ^ ((c ^ d ^ e) ^ (c ^ d ^ e)) = a ^ b ^ 0 = a ^ b.

假设 num1 = [a, b, c], nums2 = [d, e, f], 前半部分相同, 但是 ans = (a ^ (d ^ e ^ f)) ^ (b ^ (d ^ e ^ f)) ^ (c ^ (d ^ e ^ f)) = (a ^ b ^ c) ^ ((d ^ e ^ f) ^ (d ^ e ^ f) ^ (d ^ e ^ f)) = (a ^ b ^ c) ^ (d ^ e ^ f)

假设 nums1 = [a, b, c], nums2 = [d, e], ans = (a ^ d) ^ (a ^ e) ^ (b ^ d) ^ (b ^ e) ^ (c ^ d) ^ (c ^ e) = (d ^ e) ^ (d ^ e) ^ (d ^ e) = d ^ e

假设 nums1 = [a, b], nums2 = [c, d], ans = (a ^ c) ^ (a ^ d) ^ (b ^ c) ^ (b ^ d) = (c ^ d) ^(c ^ d) = 0

所以根据上面的四种情况, 我们可以大胆的推测:

  1. 当 nums1.len()与 nums2.len()都为偶数时, ans=0
  2. 当 nums1.len()为偶数时, ans=nums1[0] ^ nums1[1] ^ … nums1[n-1] ^ nums1[n]
  3. 当 nums2.len()为偶数时, ans=nums2[0] ^ nums2[1] ^ … nums2[n-1] ^ nums2[n]
  4. 当 nums1.len()与 nums2.len()均为奇数时, ans = (nums1[0] ^ nums1[1] ^ … nums1[n-1] ^ nums1[n]) ^ (nums2[0] ^ nums2[1] ^ … nums2[n-1] ^ nums2[n])

impl Solution {
    pub fn xor_all_nums(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
        if nums1.len() % 2 == 0 && nums2.len() % 2 == 0 {
            return 0;
        }
        if nums1.len() % 2 == 0 {
            return nums1.iter().fold(0, |mut s, &v| {
                s ^= v;
                s
            });
        }
        if nums2.len() % 2 == 0 {
            return nums2.iter().fold(0, |mut s, &v| {
                s ^= v;
                s
            });
        }
        let x1 = nums1.iter().fold(0, |mut s, &v| {
            s ^= v;
            s
        });
        let x2 = nums2.iter().fold(0, |mut s, &v| {
            s ^= v;
            s
        });
        x1 ^ x2
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值