返回介绍

solution / 0200-0299 / 0260.Single Number III / README_EN

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

260. Single Number III

中文文档

Description

Given an integer array nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in any order.

You must write an algorithm that runs in linear runtime complexity and uses only constant extra space.

 

Example 1:

Input: nums = [1,2,1,3,2,5]
Output: [3,5]
Explanation:  [5, 3] is also a valid answer.

Example 2:

Input: nums = [-1,0]
Output: [-1,0]

Example 3:

Input: nums = [0,1]
Output: [1,0]

 

Constraints:

  • 2 <= nums.length <= 3 * 104
  • -231 <= nums[i] <= 231 - 1
  • Each integer in nums will appear twice, only two integers will appear once.

Solutions

Solution 1: Bitwise Operation

The XOR operation has the following properties:

  • Any number XOR 0 is still the original number, i.e., $x \oplus 0 = x$;
  • Any number XOR itself is 0, i.e., $x \oplus x = 0$;

Since all numbers in the array appear twice except for two numbers, we can perform XOR operation on all numbers in the array to get the XOR result of the two numbers that only appear once.

Since these two numbers are not equal, there is at least one bit that is 1 in the XOR result. We can use the lowbit operation to find the lowest bit of 1 in the XOR result, and divide all numbers in the array into two groups based on whether this bit is 1 or not. This way, the two numbers that only appear once are separated into different groups.

Perform XOR operation on each group separately to obtain the two numbers $a$ and $b$ that only appear once.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$.

class Solution:
  def singleNumber(self, nums: List[int]) -> List[int]:
    xs = reduce(xor, nums)
    a = 0
    lb = xs & -xs
    for x in nums:
      if x & lb:
        a ^= x
    b = xs ^ a
    return [a, b]
class Solution {
  public int[] singleNumber(int[] nums) {
    int xs = 0;
    for (int x : nums) {
      xs ^= x;
    }
    int lb = xs & -xs;
    int a = 0;
    for (int x : nums) {
      if ((x & lb) != 0) {
        a ^= x;
      }
    }
    int b = xs ^ a;
    return new int[] {a, b};
  }
}
class Solution {
public:
  vector<int> singleNumber(vector<int>& nums) {
    long long xs = 0;
    for (int& x : nums) {
      xs ^= x;
    }
    int lb = xs & -xs;
    int a = 0;
    for (int& x : nums) {
      if (x & lb) {
        a ^= x;
      }
    }
    int b = xs ^ a;
    return {a, b};
  }
};
func singleNumber(nums []int) []int {
  xs := 0
  for _, x := range nums {
    xs ^= x
  }
  lb := xs & -xs
  a := 0
  for _, x := range nums {
    if x&lb != 0 {
      a ^= x
    }
  }
  b := xs ^ a
  return []int{a, b}
}
function singleNumber(nums: number[]): number[] {
  const xs = nums.reduce((a, b) => a ^ b);
  const lb = xs & -xs;
  let a = 0;
  for (const x of nums) {
    if (x & lb) {
      a ^= x;
    }
  }
  const b = xs ^ a;
  return [a, b];
}
impl Solution {
  pub fn single_number(nums: Vec<i32>) -> Vec<i32> {
    let xs = nums.iter().fold(0, |r, v| r ^ v);
    let lb = xs & -xs;
    let mut a = 0;
    for x in &nums {
      if (x & lb) != 0 {
        a ^= x;
      }
    }
    let b = xs ^ a;
    vec![a, b]
  }
}
/**
 * @param {number[]} nums
 * @return {number[]}
 */
var singleNumber = function (nums) {
  const xs = nums.reduce((a, b) => a ^ b);
  const lb = xs & -xs;
  let a = 0;
  for (const x of nums) {
    if (x & lb) {
      a ^= x;
    }
  }
  const b = xs ^ a;
  return [a, b];
};
public class Solution {
  public int[] SingleNumber(int[] nums) {
    int xs = nums.Aggregate(0, (a, b) => a ^ b);
    int lb = xs & -xs;
    int a = 0;
    foreach(int x in nums) {
      if ((x & lb) != 0) {
        a ^= x;
      }
    }
    int b = xs ^ a;
    return new int[] {a, b};
  }
}

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

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

发布评论

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