当前位置:网站首页>day260:只出现一次的数字 III
day260:只出现一次的数字 III
2022-06-22 09:19:00 【浅浅望】
问题:
给定一个整数数组 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]
来源:力扣(LeetCode)
题解
思路一:哈希表
利用哈希表存储整型数组中所有元素的个数
提取出个数为1的元素
class Solution {
public int[] singleNumber(int[] nums) {
Map<Integer, Integer> nums_count = new HashMap<>();
for(int i : nums)
nums_count.put(i,nums_count.getOrDefault(i,0)+1);
int[] a = new int[2];
int idx = 0;
for(int i : nums){
if(nums_count.get(i)==1)
a[idx++] = i;
}
return a;
}
}
思路二:异或
已知:0与整数异或的结果为整数本身,两个相同的数异或结果为0。
- 对该整数数组元素循环异或,得到只出现一次两元素的异或结果sum;
- 任选sum二进制数为1的位数k,sum中为1表明此位数上的只出现一次元素的该位的值不同,以此区分两个只出现一次的元素;
- 通过第k位不相同,将数组元素分为两个部分(每个部分仅包含一个单一元素),重新进行异或操作,得到两个只出现一次的元素a[0],a[1]。
class Solution {
public int[] singleNumber(int[] nums) {
int sum = 0;
for(int i : nums) sum ^= i;
int k = -1;
for(int i = Integer.toBinaryString(sum).length(); i>0; i--){
if(((sum >> i) & 1) == 1) k = i;
}
int[] a = new int[2];
for(int i : nums){
if((( i >> k ) & 1) ==1) a[0] ^= i;
else a[1] ^= i;
}
return a;
}
}
边栏推荐
- Win+sublime Text3 + go 1.9.2 environment setup diagram
- 使用ELK保存Syslog、Netflow日志和审计网络接口流量
- 看看volatile你深知多少
- Try/finally --return those things
- Sound and shadow 2022 heavy release! Detailed explanation of the new functions of Huisheng Huiying 2022
- Philosopher‘s Walk Gym 分治+分形
- How C processes use non static methods
- 重写?重载?你晕了吗?
- C语言刷题 | 输入一个数输出对应的值(13)
- simple_ Strtoull character conversion related functions
猜你喜欢
![[uni app] actual combat summary (including multi terminal packaging)](/img/35/6b0672311b033bc89f1518dd794777.png)
[uni app] actual combat summary (including multi terminal packaging)

Payment order case construction

User insight into the video industry in January 2022: active users began to pick up under the influence of holidays

逻辑回归和线性回归

A readme of an old open source person - how to do open source well
![[node] node+ SMS API to realize verification code login](/img/92/acd2abf73a7b0459127dd19bf32a7c.png)
[node] node+ SMS API to realize verification code login

VMware installation Kali

Servlet的生命周期

It is hoped that more and more women will engage in scientific and technological work

Debian10配置RSyslog+LoganAlyzer日志服务器
随机推荐
Alibaba big fish SMS interface PHP version, simplified version Alibaba big fish SMS sending interface PHP instance
C language question brushing | input a number and output the corresponding value (13)
Express bird of Express query demonstration code (php+curl)
[node] scaffolding server to complete token verification
Brush questions in C language | judge whether a certain year is only a leap year (15)
container_ of
实现AD环境下多用户隔离FTP
Lexical Sign Sequence
接口抽象类的比较
C语言刷题 | 温度转换(11)
通过docker安装mysql(5.7+8.0)并配置主从复制(GTID+增强半同步)
Comparison of interface abstract classes
支付-订单案例构建
[node] node+ SMS API to realize verification code login
Philosopher‘s Walk Gym 分治+分形
Stream流创建_操作_收集_案例
性能优化专题
[network security officer] an attack technology that needs to be understood - high hidden and high persistent threats
变量那些事
嵌入式开发专业术语概念汇总