当前位置:网站首页>从103个数中找到出现一次的那三个数
从103个数中找到出现一次的那三个数
2022-07-21 17:51:00 【fksfdh】
1、从101个数中找到出现一次的那1个数?
eg:[5,7,6,7,5] 找到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("出现一次的那个数=%d\n", result);
}
通过异或操作,就可以找到唯一出现的那个数;
2、从102个数中找到出现一次的那2个数?
eg:[5,7,6,7,5,8] 找到 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("出现一次的那个数=%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("第一个数=%d,第二个数=%d\n", res1,res2);
}
1、通过所有值得异或就可以找到在两个不同数之间的不同;
2、通过flag & (-flag)操作可以找到所有值得异或结果的最低位的1;
3、通过和最低位的1与操作,可以把两个不同的数分开;一个为真,一个为假;
3、从103个数中找到出现一次的那3个数?
eg: [5,7,6,7,5,8,9]找到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("第二个数=%d,第三个数=%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++;
}
}
//判断偶数是否大于0
if (count1 % 2 == 0 && res1!=0) {
printf("第一个数=%d", res2);
demo02(arr, res1, res2);
break;
}
else if (count2 % 2 == 0 && res2 != 0) {
printf("第一个数=%d", res1);
demo02(arr, res2, res1);
break;
}
//如果没找到 0 3
flag << 1;
}
}
1、首先,使用暴力法,从1开始,每次向左移动1位二进制位,到32位也就是移动31位;
2、就把把那三个单独的数分成2堆,只有两种情况;奇数堆:偶数堆 = 3 :0 或 1:2;
3、只需要判断偶数堆的异或操作不为0,也就是说是1:2这种情况;就找到了在奇数堆中的一个单数;
4、通过demo02函数;思想和第2题一样,但是因为有103个数,所以第三个单数会被分配到其中一边;
5、我们需要判断这个数,是在奇数堆还是在偶数堆,通过split_val & odd判断是在那一边,就可以在异或一次,就可以消除这个数;
边栏推荐
猜你喜欢
QT configuration opencv (II): successful
各地级市-进出口与贸易差额(2000-2020)
移除iPhone手机删不掉的绿色logo的百宝箱App
JVM 简介
服务器内存故障预测居然可以这样做!
QT中配置OpenCV
栈和队列常见oj题
Ctrip spark multi tenant query service evolution, Apache Kyuubi can be expected in the future
流
Leetcode83 & leetcode82: delete duplicate elements in the sorting linked list
随机推荐
js执行机制
QT配置OpenCV(二):成功
前辈的前后台分离介绍
数据格式化小组件
"Everything is interconnected, enabling thousands of industries", the 2022 open atom global open source summit openatom openharmony sub forum is about to open
浏览器类型判断
对称的res,aes,非对称rsa(jwt)的简单使用及区别
模拟实现库函数memcpy--复制内存块。详细理解内存重叠及精准复制问题
Seven ways to create thread pools
CAN通信协议(一)
Leetcode-12: integer to Roman numeral
Advertisements inserted in solo articles are not displayed
QT中配置OpenCV
OpenGL:freeglut ERROR: Function <glutCreateWindow> called without first calling ‘glutInit‘.
推荐系统-协同过滤在Spark中的实现
js获取对象属性值
并行与并发
Ctrip spark multi tenant query service evolution, Apache Kyuubi can be expected in the future
Is Huatai app safe to open an account, and is the Commission very high
解决按钮UIButton重复点击问题