返回介绍

solution / 2500-2599 / 2527.Find Xor-Beauty of Array / README_EN

发布于 2024-06-17 01:03:04 字数 3257 浏览 0 评论 0 收藏 0

2527. Find Xor-Beauty of Array

中文文档

Description

You are given a 0-indexed integer array nums.

The effective value of three indices i, j, and k is defined as ((nums[i] | nums[j]) & nums[k]).

The xor-beauty of the array is the XORing of the effective values of all the possible triplets of indices (i, j, k) where 0 <= i, j, k < n.

Return _the xor-beauty of_ nums.

Note that:

  • val1 | val2 is bitwise OR of val1 and val2.
  • val1 & val2 is bitwise AND of val1 and val2.

 

Example 1:

Input: nums = [1,4]
Output: 5
Explanation: 
The triplets and their corresponding effective values are listed below:
- (0,0,0) with effective value ((1 | 1) & 1) = 1
- (0,0,1) with effective value ((1 | 1) & 4) = 0
- (0,1,0) with effective value ((1 | 4) & 1) = 1
- (0,1,1) with effective value ((1 | 4) & 4) = 4
- (1,0,0) with effective value ((4 | 1) & 1) = 1
- (1,0,1) with effective value ((4 | 1) & 4) = 4
- (1,1,0) with effective value ((4 | 4) & 1) = 0
- (1,1,1) with effective value ((4 | 4) & 4) = 4 
Xor-beauty of array will be bitwise XOR of all beauties = 1 ^ 0 ^ 1 ^ 4 ^ 1 ^ 4 ^ 0 ^ 4 = 5.

Example 2:

Input: nums = [15,45,20,2,34,35,5,44,32,30]
Output: 34
Explanation: The xor-beauty of the given array is 34.

 

Constraints:

  • 1 <= nums.length <= 105
  • 1 <= nums[i] <= 109

Solutions

Solution 1

class Solution:
  def xorBeauty(self, nums: List[int]) -> int:
    return reduce(xor, nums)
class Solution {
  public int xorBeauty(int[] nums) {
    int ans = 0;
    for (int x : nums) {
      ans ^= x;
    }
    return ans;
  }
}
class Solution {
public:
  int xorBeauty(vector<int>& nums) {
    int ans = 0;
    for (auto& x : nums) {
      ans ^= x;
    }
    return ans;
  }
};
func xorBeauty(nums []int) (ans int) {
  for _, x := range nums {
    ans ^= x
  }
  return
}
function xorBeauty(nums: number[]): number {
  return nums.reduce((acc, cur) => acc ^ cur, 0);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文