当前位置:网站首页>LeetCode_位运算_中等_260.只出现一次的数字 III
LeetCode_位运算_中等_260.只出现一次的数字 III
2022-07-28 18:18:00 【小城老街】
1.题目
给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次。找出只出现一次的那两个元素。你可以按任意顺序 返回答案。
进阶:你的算法应该具有线性时间复杂度。你能否仅使用常数空间复杂度来实现?
示例 1:
输入:nums = [1,2,1,3,2,5]
输出:[3,5]
解释:[5, 3] 也是有效的答案。
示例 2:
输入:nums = [-1,0]
输出:[-1,0]
示例 3:
输入:nums = [0,1]
输出:[1,0]
提示:
2 <= nums.length <= 3 * 104
-231 <= nums[i] <= 231 - 1
除两个只出现一次的整数外,nums 中的其他数字都出现两次
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/single-number-iii
2.思路
(1)哈希表
如果使用额外空间的话,比较容易想到的是使用哈希表,先将数组中的所有元素添加到 hashMap 中(键/值为元素值/该元素出现的次数),然后再在哈希表中找出只出现了一次的元素并将其返回即可。
(2)位运算
思路参考该 LeetCode 用户题解。
下面举个例子来说明该方法的过程:
① 假设数组 nums = {1, 2, 1, 3, 2, 5},其中的元素对应的二进制数分别为:
00…001
00…010
00…001
00…011
00…010
00…101
将它们进行异或操作,得到的结果为 xorSum = 1 ^ 2 ^ 1 ^ 3 ^ 2 ^ 5 = 3 ^ 5 = (00…011 ^ 00…101) = 00…110。
② 现在任取 xorSum 二进制表示中为 1 一位 k,例如 k = 2,这意味着数组 nums 中只出现了一次的元素的二进制表示的第 k 位不相同,此处对应 00…011 和 00…101。
③ 对 nums 进行遍历,对第 k 位分别为 0 和 1 的元素分别求异或和,这样两答案必然会被分到不同的组,具体过程如下:
数组 nums 中二进制表示的第 2 位为 0 的元素有 1, 2, 1, 3, 2
数组 nums 中二进制表示的第 2 位为 1 的元素有 5
1 ^ 2 ^ 1 ^ 3 ^ 2 = 3
5 = 5
由于对于任意数 a,a ^ 0 = a,所以为了方便保存结果,可先定义长度为 2 的结果数组 res,其初始值均为 0,那么
res[0] = res[0] ^ 1 ^ 2 ^ 1 ^ 3 ^ 2 = 3
res[1] = res[1] ^ 5 = 5
这样便找到了数组 nums 中只出现一次的两个元素!
相似题目:
LeetCode_位运算_简单_136.只出现一次的数字
LeetCode_位运算_中等_137.只出现一次的数字 II
3.代码实现(Java)
//思路1————哈希表
class Solution {
public int[] singleNumber(int[] nums) {
HashMap<Integer, Integer> hashMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
hashMap.put(nums[i], hashMap.getOrDefault(nums[i], 0) + 1);
}
int cnt = 0;
int[] res = new int[2];
for (Integer num : hashMap.keySet()) {
if (hashMap.get(num) == 1) {
res[cnt++] = num;
}
}
return res;
}
}
//思路2————位运算
class Solution {
public int[] singleNumber(int[] nums) {
int[] res = new int[2];
/* xorSum 是数组 nums 中的所有元素进行异或操作得到的结果 由于除两个只出现一次的整数外,数组 nums 中的其他数字都出现两次,且两个相同的数的异或 操作的结果为 0,所以 xorSum 是两个只出现一次的整数的异或值 */
int xorSum = 0;
for (int num : nums) {
xorSum ^= num;
}
int k = -1;
for (int i = 31; i >= 0 && k == -1; i--) {
if (((xorSum >> i) & 1) == 1) {
k = i;
}
}
for (int num : nums) {
if (((num >> k) & 1) == 0) {
res[0] ^= num;
} else {
res[1] ^= num;
}
}
return res;
}
}
边栏推荐
- mmo及时战斗游戏中的场景线程分配
- Basic knowledge of communication network 01
- Digital filter design matlab
- 树行表达方式
- [in depth study of 4g/5g/6g topics -44]: urllc-15 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -9-low delay technology -3-non slot scheduling mini
- Using typedef in C language to change the name of data type
- 跨区域网络的通信学习静态路由
- [C language] summary of methods for solving the greatest common divisor
- flask_ Mail source code error
- Wildcard ssl/tls certificate
猜你喜欢

七种轮询介绍(后附实践链接)

【实验分享】CCIE—BGP反射器实验

9. Pointer of C language (3) classic program, exchange the value of two numbers for deep analysis, (easy to understand), are formal parameters and arguments a variable?
![[C language] string reverse order implementation (recursion and iteration)](/img/c3/02d0a72f6026df8a67669293e55ef2.png)
[C language] string reverse order implementation (recursion and iteration)

CM4 development cross compilation tool chain production

私有化部署的即时通讯平台,为企业移动业务安全保驾护航

DSACTF7月re

C language pointer and two-dimensional array
![[C language] 5000 word super detailed explanation of various operations of the sequence table](/img/3b/932526e96ef14df8a321048e9c14b4.png)
[C language] 5000 word super detailed explanation of various operations of the sequence table

Why is customer support important to SaaS?
随机推荐
Solve flask integration_ Error reporting in restplus
C language - pointer
Merge sort template
Stories of Party members | Li qingai uses cartoons to drive farmers to increase income and become rich
flask_ Mail source code error
ssm中项目异常处理
plt. What does it mean when linestyle, marker, color equals none in plot()
进制及数的表示 2
Raspberry pie 4B parsing PWM
4. Const and difine and the problem of initializing arrays with const and define
字符设备驱动结构
Raspberry pie 4B ffmpeg RTMP streaming
Solve the cookie splitting problem (DP)
Implementation of strstr in C language
Richpedia: A Large-Scale, Comprehensive Multi-Modal Knowledge Graph
基于 MinIO 对象存储保障 Rancher 数据
Advanced notes (Part 2)
A chip company fell in round B
The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!
Solve the kangaroo crossing problem (DP)