九度OJ 1351 数组中只出现一次的数字
题目地址:http://ac.jobdu.com/problem.php?pid=1351
- 题目描述:
-
一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
- 输入:
-
每个测试案例包括两行:第一行包含一个整数n,表示数组大小。2<=n <= 10^6。第二行包含n个整数,表示数组元素,元素均为int。
- 输出:
-
对应每个测试案例,输出数组中只出现一次的两个数。输出的数字从小到大的顺序。
- 样例输入:
-
8
2 4 3 6 3 2 5 5
- 样例输出:
-
4 6
#include <stdio.h>
unsigned int FindFirstBitIs1 (int num){
int indexBit = 0;
while ((num & 1) == 0 && indexBit < 32){
num >>= 1;
++indexBit;
}
return indexBit;
}
int IsBit1 (int data, unsigned int indexof1){
data >>= indexof1;
return data & 0x01;
}
void FindNumsAppearOnce (int data[], int n, int * num1, int * num2){
int result = 0;
int i;
for (i=0; i<n; ++i){
result ^= data[i];
}
unsigned int indexof1 = FindFirstBitIs1 (result);
*num1 = 0;
*num2 = 0;
for (i=0; i<n; ++i){
if (IsBit1 (data[i], indexof1))
*num1 ^= data[i];
else
*num2 ^= data[i];
}
}
int main(void){
int n;
int input[1000000];
int i;
int num1;
int num2;
while (scanf ("%d", &n) != EOF){
for (i=0; i<n; ++i)
scanf ("%d", &input[i]);
FindNumsAppearOnce (input, n, &num1, &num2);
if (num1 < num2)
printf ("%d %d\n", num1, num2);
else
printf ("%d %d\n", num2, num1);
}
return 0;
}
参考资料:何海涛 -- 程序员面试题精选100题(34)-数组中只出现一次的数字[算法]
九度OJ 1351 数组中只出现一次的数字的更多相关文章
- 剑指Offer - 九度1351 - 数组中只出现一次的数字
剑指Offer - 九度1351 - 数组中只出现一次的数字2013-11-23 01:23 题目描述: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. ...
- 九度OJ 1348 数组中的逆序对 -- 归并排序
题目地址:http://ac.jobdu.com/problem.php?pid=1348 题目描述: 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求 ...
- 九度OJ 1534 数组中第K小的数字 -- 二分查找
题目地址:http://ac.jobdu.com/problem.php?pid=1534 题目描述: 给定两个整型数组A和B.我们将A和B中的元素两两相加可以得到数组C. 譬如A为[1,2],B为[ ...
- 九度OJ 1370 数组中出现次数超过一半的数字
题目地址:http://ac.jobdu.com/problem.php?pid=1370 题目描述: 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2 ...
- 九度oj 1348 数组中的逆序对
原题链接:http://ac.jobdu.com/problem.php?pid=1348 归并排序求逆序对... #include<algorithm> #include<iost ...
- 剑指offer:数组中只出现一次的数字
题目描述: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字. 思路分析: 1. 直接想法,每个数字遍历,统计出现次数,复杂度O(n^2),超时. 2. 借助 ...
- leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)
136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...
- 剑指Offer 40. 数组中只出现一次的数字 (数组)
题目描述 一个整型数组里除了两个数字之外,其他的数字都出现了偶数次.请写程序找出这两个只出现一次的数字. 题目地址 https://www.nowcoder.com/practice/e02fdb54 ...
- 【剑指Offer】40、数组中只出现一次的数字
题目描述: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字.要求时间复杂度为O(n),空间复杂度为O(1). 解题思路: 这道题目相对比较难 ...
随机推荐
- c++中类长度解析
通常我们定义一个类,它所占的空间有多大呢? 首先我们看一下下面的这个类 class A{ public: void func1(void){ printf("11111heihei\n&qu ...
- UVA 705 Slash Maze
Slash Maze By filling a rectangle with slashes (/) and backslashes ( ), you can generate nice litt ...
- Update操作浅析,一定是先Delete再Insert吗?
Update操作一定是先Delete再Insert吗? Update在数据库中的执行是怎么样的?“Update操作是先把数据删除,然后再插入数据”.在网上看了很多也都是这么认为的.但在查阅到一些不同看 ...
- javascript---遇到关于this的相关问题(解决this)(持续更新中...)
1.在原型中使用this <!doctype html> <html lang="en"> <head> <meta charset=&q ...
- 图片轮换cycle插件的运用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 50个Android开发技巧(02 延迟载入和避免反复渲染视图)
当你在Application中创建复杂的布局时.页面的渲染过程也变得更加缓慢. 此时,我们须要利用 <include />标签(避免反复渲染)和 ViewStub类(延迟载入)来优化我们的 ...
- 编写程序,查找并删除forward_list<int>中的奇数元素
#include<iostream> #include<forward_list> using namespace std; int main() { forward_list ...
- excel vba 打印设置(转)
FROM: http://hi.baidu.com/kdlipm/blog/item/0897dd16ffc03e59f3de32ab.html PageSetup 函式就會記錄時, 設定的記錄三個部 ...
- linux cpio 备份命令
cpio 命令 [root@linux ~]# cpio -covB > [file|device] <==备份[root@linux ~]# cpio -icduv < [fi ...
- Debug 之 The state information is invalid for this page and might be corrupted
1.问题描述: 网站部署之后,排序或者搜索之后报错:The state information is invalid for this page and might be corrupted 2.问题 ...