剑指 Offer 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
解答:
class Solution {
//分组异或
//时间复杂度O(N),空间复杂度O(1)
public int[] singleNumbers(int[] nums) {
int ans = 0;
for(int num : nums) ans ^= num;
int div = 1;
while((div & ans) == 0) div <<= 1;
int a = 0, b = 0;
for(int num : nums){
if((div & num) != 0) a ^= num;
else b ^= num;
}
return new int[]{a, b};
}
}
参考自:
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-lcof/solution/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。