返回介绍

solution / 2300-2399 / 2317.Maximum XOR After Operations / README_EN

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

2317. Maximum XOR After Operations

中文文档

Description

You are given a 0-indexed integer array nums. In one operation, select any non-negative integer x and an index i, then update nums[i] to be equal to nums[i] AND (nums[i] XOR x).

Note that AND is the bitwise AND operation and XOR is the bitwise XOR operation.

Return _the maximum possible bitwise XOR of all elements of _nums_ after applying the operation any number of times_.

 

Example 1:

Input: nums = [3,2,4,6]
Output: 7
Explanation: Apply the operation with x = 4 and i = 3, num[3] = 6 AND (6 XOR 4) = 6 AND 2 = 2.
Now, nums = [3, 2, 4, 2] and the bitwise XOR of all the elements = 3 XOR 2 XOR 4 XOR 2 = 7.
It can be shown that 7 is the maximum possible bitwise XOR.
Note that other operations may be used to achieve a bitwise XOR of 7.

Example 2:

Input: nums = [1,2,3,9,2]
Output: 11
Explanation: Apply the operation zero times.
The bitwise XOR of all the elements = 1 XOR 2 XOR 3 XOR 9 XOR 2 = 11.
It can be shown that 11 is the maximum possible bitwise XOR.

 

Constraints:

  • 1 <= nums.length <= 105
  • 0 <= nums[i] <= 108

Solutions

Solution 1

class Solution:
  def maximumXOR(self, nums: List[int]) -> int:
    return reduce(or_, nums)
class Solution {
  public int maximumXOR(int[] nums) {
    int ans = 0;
    for (int x : nums) {
      ans |= x;
    }
    return ans;
  }
}
class Solution {
public:
  int maximumXOR(vector<int>& nums) {
    int ans = 0;
    for (int& x : nums) {
      ans |= x;
    }
    return ans;
  }
};
func maximumXOR(nums []int) (ans int) {
  for _, x := range nums {
    ans |= x
  }
  return
}
function maximumXOR(nums: number[]): number {
  let ans = 0;
  for (const x of nums) {
    ans |= x;
  }
  return ans;
}

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

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

发布评论

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