260. Single Number III

LeetCode medium original: C# #array #bit-manipulation #csharp #leetcode #medium
题目文本会按所选界面语言从俄语翻译;代码保持不变。

Дан 整数 数组 nums, в котором ровно два elementа встречаются только один раз, а все остальные elementы встречаются ровно дважды. find два elementа, которые встречаются только один раз. Вы можете вернуть ответ в любом порядке.

C# 解法

匹配/原始
public class Solution {
    public int[] SingleNumber(int[] nums) {
        int xor = 0;
        foreach (int num in nums) {
            xor ^= num;
        }
        int diff = xor & -xor;
        int[] res = new int[2];
        foreach (int num in nums) {
            if ((num & diff) == 0) {
                res[0] ^= num;
            } else {
                res[1] ^= num;
            }
        }
        return res;
    }
}

C++ 解法

自动草稿,提交前请检查
#include <bits/stdc++.h>
using namespace std;

// Auto-generated C++ draft from the C# solution. Review containers, LINQ and helper types before submit.
class Solution {
public:
    public vector<int>& SingleNumber(vector<int>& nums) {
        int xor = 0;
        foreach (int num in nums) {
            xor ^= num;
        }
        int diff = xor & -xor;
        vector<int>& res = new int[2];
        foreach (int num in nums) {
            if ((num & diff) == 0) {
                res[0] ^= num;
            } else {
                res[1] ^= num;
            }
        }
        return res;
    }
}

Java 解法

匹配/原始
class Solution {
    public int[] singleNumber(int[] nums) {
        int xor = 0;
        for (int num : nums) {
            xor ^= num;
        }
        int diff = xor & -xor;
        int[] res = new int[2];
        for (int num : nums) {
            if ((num & diff) == 0) {
                res[0] ^= num;
            } else {
                res[1] ^= num;
            }
        }
        return res;
    }
}

JavaScript 解法

匹配/原始
class Solution {
    singleNumber(nums) {
        let xor = 0
        for (const num of nums) {
            xor ^= num
        }
        const diff = xor & -xor
        const res = [0, 0]
        for (const num of nums) {
            if ((num & diff) === 0) {
                res[0] ^= num
            } else {
                res[1] ^= num
            }
        }
        return res
    }
}

Python 解法

匹配/原始
class Solution:
    def singleNumber(self, nums: List[int]) -> List[int]:
        xor = 0
        for num in nums:
            xor ^= num
        diff = xor & -xor
        res = [0, 0]
        for num in nums:
            if num & diff == 0:
                res[0] ^= num
            else:
                res[1] ^= num
        return res

Go 解法

匹配/原始
func singleNumber(nums []int) []int {
    xor := 0
    for _, num := range nums {
        xor ^= num
    }
    diff := xor & -xor
    res := []int{0, 0}
    for _, num := range nums {
        if num&diff == 0 {
            res[0] ^= num
        } else {
            res[1] ^= num
        }
    }
    return res
}

Algorithm

示例:

Input: nums = [1,2,1,3,2,5]

Output: [3,5]

Explanation: [5, 3] is also a valid answer.

👨‍💻

算法:

1️⃣

Выполните XOR для всех elementов 数组а nums. Это даст результат, который является XOR двух уникальных чисел.

2️⃣

find бит, который отличается в этих двух числах, чтобы разделить все числа в 数组е на две группы.

3️⃣

Выполните XOR для каждой группы, чтобы find два уникальных числа.

😎

Vacancies for this task

活跃职位 with overlapping task tags are 已显示.

所有职位
目前还没有活跃职位。