56 - I. 数组中数字出现的次数


comments: true

difficulty: 中等
edit_url: https://github.com/doocs/leetcode/edit/main/lcof/面试题56 - I. 数组中数字出现的次数/README.md

面试题 56 - I. 数组中数字出现的次数

题目描述

一个整型数组 nums 里除两个数字之外,其他数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度是O(n),空间复杂度是O(1)。

示例 1:

复制代码
输入:nums = [4,1,4,6]
输出:[1,6] 或 [6,1]

示例 2:

复制代码
输入:nums = [1,2,10,4,1,4,3,3]
输出:[2,10] 或 [10,2]

限制:

  • 2 <= nums.length <= 10000

解法

方法一:位运算

由于数组中除了两个数字之外,其他数字都出现了两次,因此对数组中的所有数字进行异或运算,得到的结果即为两个只出现一次的数字的异或结果。

由于这两个数字不相等,因此异或结果中至少存在一位为 1 1 1。我们通过 lowbit 运算找到异或结果中最低位的 1 1 1,并将数组中的所有数字按照该位是否为 1 1 1 分为两组,这样两个只出现一次的数字就被分到了不同的组中。

对两个组分别进行异或运算,即可得到两个只出现一次的数字。

时间复杂度 O ( n ) O(n) O(n),其中 n n n 为数组长度。空间复杂度 O ( 1 ) O(1) O(1)。

https://www.acwing.com/video/2852/

Python3
python 复制代码
# reduce 函数将一个二元函数(即接受两个参数的函数)逐步应用到可迭代对象的项上,从左到右,直到将可迭代对象缩减为一个单一的值。
 from functools import reduce
class Solution:
    def singleNumbers(self, nums: List[int]) -> List[int]:
        xs = reduce(xor, nums) #所有元素的异或结果=两个不相等数的异或结果a&b
        
        a = 0
        lb = xs & -xs  #得到最后一个位为1对应的数: 两个不相等数的异或结果a&b ≠ 0,说明至少有一位为1
        for x in nums:
            if x & lb:  #核心1:这样运算 只会剩下 第k位为1的a , 其他次数为2的数异或为0
                a ^= x
        b = xs ^ a   #核心2:获得a后,xs^a 会将a对应的位抹零,剩下的位就属于b
        return [a, b]
Java
java 复制代码
class Solution {
    public int[] singleNumbers(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};
    }
}
C++
cpp 复制代码
class Solution {
public:
    vector<int> singleNumbers(vector<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) {
                a ^= x;
            }
        }
        int b = xs ^ a;
        return {a, b};
    }
};
Go
go 复制代码
func singleNumbers(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}
}
TypeScript
ts 复制代码
function singleNumbers(nums: number[]): number[] {
    let xs = 0;
    for (const x of nums) {
        xs ^= x;
    }
    const lb = xs & -xs;
    let a = 0;
    for (const x of nums) {
        if (x & lb) {
            a ^= x;
        }
    }
    const b = xs ^ a;
    return [a, b];
}
JavaScript
js 复制代码
/**
 * @param {number[]} nums
 * @return {number[]}
 */
var singleNumbers = function (nums) {
    let xs = 0;
    for (const x of nums) {
        xs ^= x;
    }
    const lb = xs & -xs;
    let a = 0;
    for (const x of nums) {
        if (x & lb) {
            a ^= x;
        }
    }
    const b = xs ^ a;
    return [a, b];
};
C#
cs 复制代码
public class Solution {
    public int[] SingleNumbers(int[] nums) {
        int xs = 0;
        foreach(int x in nums) {
            xs ^= x;
        }
        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};
    }
}
Swift
swift 复制代码
class Solution {
    func singleNumbers(_ nums: [Int]) -> [Int] {
        var xorSum = 0
        for num in nums {
            xorSum ^= num
        }

        let lowBit = xorSum & -xorSum
        var a = 0
        for num in nums {
            if (num & lowBit) != 0 {
                a ^= num
            }
        }

        let b = xorSum ^ a
        return [a, b]
    }
}
相关推荐
还有糕手28 分钟前
算法【有依赖的背包】
算法·动态规划
pursuit_csdn1 小时前
力扣 347. 前 K 个高频元素
算法·leetcode
wen__xvn1 小时前
每日一题洛谷B3865 [GESP202309 二级] 小杨的 X 字矩阵c++
c++·算法·矩阵
makabaka_T_T1 小时前
25寒假算法刷题 | Day1 | LeetCode 240. 搜索二维矩阵 II,148. 排序链表
数据结构·c++·算法·leetcode·链表·矩阵
辞半夏丶北笙2 小时前
最近最少使用算法(LRU最近最少使用)缓存替换算法
java·算法·缓存
BingLin-Liu2 小时前
蓝桥杯备考:六大排序算法
算法·排序算法
南玖yy2 小时前
C语言:数组的介绍与使用
c语言·开发语言·算法
小菜鸟博士2 小时前
手撕Vision Transformer -- Day1 -- 基础原理
人工智能·深度学习·学习·算法·面试
灰灰老师3 小时前
数据分析系列--[11] RapidMiner,K-Means聚类分析(含数据集)
人工智能·算法·机器学习·数据挖掘·数据分析·kmeans·rapidminer
追求源于热爱!4 小时前
记4(可训练对象+自动求导机制+波士顿房价回归预测
图像处理·人工智能·算法·机器学习·回归