当前位置:网站首页>Find the three numbers that appear once from the 103 numbers
Find the three numbers that appear once from the 103 numbers
2022-07-22 09:20:00 【fksfdh】
1、 from 101 Find the one that appears once in the number 1 Number ?
eg:[5,7,6,7,5] find 6
//101
void demo01() {
int arr[5] = {
5,7,6,7,5 };
int result;
result = 0;
for (int i = 0; i < 5; i++) {
result ^= arr[i];
}
printf(" The number that appears once =%d\n", result);
}
By XOR operation , You can find the only number that appears ;
2、 from 102 Find the one that appears once in the number 2 Number ?
eg:[5,7,6,7,5,8] find 6,8
//101
int demo01(int arr[]) {
//int arr[5] = { 5,7,6,7,5 };
int result;
result = 0;
for (int i = 0; i < 6; i++) {
result ^= arr[i];
}
//printf(" The number that appears once =%d\n", result);
return result;
}
//102
void demo02() {
int arr[6] = {
5,7,6,7,5,8 };
int flag = demo01(arr);
int split_val = flag & (-flag);
printf("%d\n", split_val);
int res1, res2;
res1 = 0; res2 = 0;
for (int i = 0; i < 6; i++) {
if (arr[i]&split_val) {
res1 ^= arr[i];
}else {
res2 ^= arr[i];
}
}
printf(" First number =%d, The second number =%d\n", res1,res2);
}
1、 The difference between two different numbers can be found through all valued XORs ;
2、 adopt flag & (-flag) Operation can find the lowest value of all XOR results 1;
3、 Pass and lowest 1 And operation , You can separate two different numbers ; One is true , One is false ;
3、 from 103 Find the one that appears once in the number 3 Number ?
eg: [5,7,6,7,5,8,9] find 6,8,9
//102
void demo02(int arr[],int even,int odd) {
int split_val = even & (-even);
//printf("%d\n", split_val);
int res1, res2;
res1 = 0; res2 = 0;
for (int i = 0; i < 7; i++) {
if (arr[i]&split_val) {
res1 ^= arr[i];
}else {
res2 ^= arr[i];
}
}
if (split_val& odd) {
res1 ^= odd;
}else {
res2 ^= odd;
}
printf(" The second number =%d, The third number =%d\n", res1,res2);
}
//103
void demo03() {
int arr[7] = {
5,7,6,7,5,8,9 };
int res1, res2,count1,count2;
int flag;
flag = 1;
for (int i = 0; i < 32;i++) {
res1=res2=count1=count2 = 0;
for (int j = 0; j < 7; j++) {
if (flag&arr[j]) {
res1 ^= arr[j];
count1++;
}else {
res2 ^= arr[j];
count2++;
}
}
// Judge whether the even number is greater than 0
if (count1 % 2 == 0 && res1!=0) {
printf(" First number =%d", res2);
demo02(arr, res1, res2);
break;
}
else if (count2 % 2 == 0 && res2 != 0) {
printf(" First number =%d", res1);
demo02(arr, res2, res1);
break;
}
// If not 0 3
flag << 1;
}
}
1、 First , Law on the use of violence , from 1 Start , Every time I move left 1 Bit binary bit , To 32 Bit means moving 31 position ;
2、 Just divide the three separate numbers 2 Pile up , There are only two cases ; Odd heap : Even heap = 3 :0 or 1:2;
3、 Just judge that the XOR operation of the even heap is not 0, That is to say 1:2 This situation ; You find a singular number in the odd heap ;
4、 adopt demo02 function ; Thoughts and 2 Same question , But because of 103 Number , So the third singular will be assigned to one side ;
5、 We need to judge this number , Is it in the odd heap or the even heap , adopt split_val & odd The judgment is on that side , Can be in XOR once , You can eliminate this number ;
边栏推荐
- About the plaintext syntax of vditor Publishing
- 腾讯游戏 :我们如何基于 StarRocks 构建云原生数仓
- The request was rejected because the URL contained a potentially malicious String “//“
- 异常处理
- 详解flex布局
- MongoDB的使用MongoTemplate操作增删改查,分页,排序,聚合(含内嵌数据),文件上传下载
- odoo-js-doAction详解
- Single cell paper record (Part5) -- a joint model of unpaired data from scrna SEQ and st for impating missing gene
- 单细胞论文记录(part5)--A joint model of unpaired data from scRNA-seq and ST for imputing missing gene ...
- 大批量数据excel下载—本文作者只试了51万数据的下载,用时7秒
猜你喜欢
真人踩过的坑,告诉你避免自动化测试新手常犯的10个错误
Tencent cloud free upgrade.
包装类(类型之间的相互转化)
About the plaintext syntax of vditor Publishing
SerializationException: Could not read JSON: Unrecognized token “xxx“
odoo-js-doAction详解
The pit trodden by real people tells you to avoid the 10 mistakes that novices in automated testing often make
Redis原理之BigKey和热点Key
Configure opencv in QT
flex布局
随机推荐
Is it safe to open an account at Huatai Securities? Where is it more reliable
拼写单词~
推荐系统-协同过滤在Spark中的实现
SerializationException: Could not read JSON: Unrecognized token “xxx“
jq移除某个元素样式
Leetcode-468: verify IP address
遇到的问题记录
"Everything is interconnected, enabling thousands of industries", the 2022 open atom global open source summit openatom openharmony sub forum is about to open
EasyCVR平台如何实现无需鉴权即可接入特殊设备?
单细胞论文记录(part17)--Integrating sc transcriptomic data across different conditions, tech and species
Pointer of C language (4)
Leetcode · 1720: array after XOR decoding
Scheme design of meal return process of performance system
LeetCode83 && LeetCode82: 删除排序链表中的重复元素
浏览器获取cookie失败,浏览器解决方案
1978-2021中国统计年鉴、2003-2019(省、市面板数据)、1999-2019(县域面板数据)
Advertisements inserted in solo articles are not displayed
Configure opencv in QT
Redis原理之GEO
QT配置OpenCV(二):成功