某整形数组中除了两个单身整数外, 其余的整数都是成对出现的, 利用C/C++代码求出这两个单身整数。 要求: 时间复杂度o(n), 空间复杂度o(1)------某公司招聘试题
先看看这个题目:某整形数组中除了两个单身整数外, 其余的整数都是成对出现的, 利用C代码求出这两个单身整数。 要求: 时间复杂度o(n), 空间复杂度o(1).
我们先用最傻瓜的方式来做吧:
#include <iostream>
using namespace std; // 时间复杂度为o(n^2), 空间复杂度为o(1), 不符合要求
void findSoleNumbers(int a[], int n, int &e1, int &e2)
{
int i = 0;
int j = 0;
int continueFlag = 0; // 控制外层for, 推断是否过滤当前整数
int numberFlag = 0; // 标志第一个、第二个单身整数 for(i = 0; i < n; i++)
{
continueFlag = 0;
for(j = 0; j < n; j++)
{
if(j != i && a[j] == a[i])
{
continueFlag = 1;
break;
}
} if(1 == continueFlag) // 该整数是成双成对的。 过滤掉
{
continue;
} // 可怜的单身整数
if(0 == numberFlag++)
{
e1 = a[i];
}
else
{
e2 = a[i];
}
}
} int main()
{
{
int a[] = {1, 5, 3, 5, 1, 2};
int n = sizeof(a) / sizeof(a[0]);
int e1 = 0;
int e2 = 0; findSoleNumbers(a, n, e1, e2);
cout << e1 << endl;
cout << e2 << endl;
cout << "--------------------------" << endl;
} {
int a[] = {1, 1, 2, 5, 4, 5, 2, 4, 3, 0};
int n = sizeof(a) / sizeof(a[0]);
int e1 = 0;
int e2 = 0; findSoleNumbers(a, n, e1, e2);
cout << e1 << endl;
cout << e2 << endl;
cout << "--------------------------" << endl;
} return 0;
}
结果为:
3
2
--------------------------
3
0
--------------------------
上面程序时间复杂度不满足题目要求。
当然, 有的朋友可能会想到排序, 思路是能够, 可是, 时间复杂度依旧不是o(n), 所以, 排序法我就不介绍了。
因为数组中整数的范围并没有给出。 所以, 也不太适合用计数的方法来做。 那怎么办呢? 假如该题目中的整形数组中仅仅有一个单身整数。 那也好办, 例如以下:
#include <iostream>
using namespace std; // 时间复杂度为o(n), 空间复杂度为o(1), 不符合要求
void findSoleNumber(int a[], int n, int &e)
{
e = 0;
int i = 0;
for(i = 0; i < n; i++)
{
e ^= a[i];
}
} int main()
{
{
int a[] = {1, 5, 3, 5, 1};
int n = sizeof(a) / sizeof(a[0]);
int e = 0; findSoleNumber(a, n, e);
cout << e << endl;
cout << "--------------------------" << endl;
} {
int a[] = {0, 5, 1, 5, 1, 2, 2};
int n = sizeof(a) / sizeof(a[0]);
int e = 0; findSoleNumber(a, n, e);
cout << e << endl;
cout << "--------------------------" << endl;
} return 0;
}
结果:
3
--------------------------
0
--------------------------
上面的方法虽然没有彻底解决这个问题, 但已经提供了思路的雏形了。 以下。 我直接给出可行的方法。 代码本身就是最好的解释, 所以不再解释。 代码例如以下:
#include <iostream>
using namespace std; // 在num的二进制中查找第一个出现1的位置
int findFirstBitEquOne(int num)
{
int bitIndex = 0;
while(bitIndex < 32 && 0 == (num & 1))
{
num >>= 1;
bitIndex++;
} return bitIndex;
} // 推断num二进制的bitIndex位上的数是否为1
bool isBitOne(int num, int bitIndex)
{
return ( (num >>= bitIndex) & 1);
} // 时间复杂度为o(n), 空间复杂度为o(1)
void findSoleNumbers(int a[], int n, int &e1, int &e2)
{
e1 = 0;
e2 = 0; int i = 0;
int result = 0;
for(i = 0; i < n; i++)
{
result ^= a[i]; // 最后的result肯定是两个单身整数的异或值
} int bitIndex = findFirstBitEquOne(result); for(i = 0; i < n; i++)
{
// 对于每个整数, 依据isBitOne原则进行分组, 两个单身整数必定落在不同的组中, 而成双成对的整数必定落在同一组中 if(isBitOne(a[i], bitIndex)) // 组1
{
//cout << "debug1: " << a[i] << endl;
e1 ^= a[i];
}
else // 组2
{
//cout << "debug2: " << a[i] << endl;
e2 ^= a[i];
}
}
} int main()
{
{
int a[] = {1, 5, 3, 5, 1, 2};
int n = sizeof(a) / sizeof(a[0]);
int e1 = 0;
int e2 = 0; findSoleNumbers(a, n, e1, e2);
cout << e1 << endl;
cout << e2 << endl;
cout << "--------------------------" << endl;
} {
int a[] = {1, 1, 2, 5, 4, 5, 2, 4, 3, 0};
int n = sizeof(a) / sizeof(a[0]);
int e1 = 0;
int e2 = 0; findSoleNumbers(a, n, e1, e2);
cout << e1 << endl;
cout << e2 << endl;
cout << "--------------------------" << endl;
} return 0;
}
结果例如以下:
3
2
--------------------------
3
0
--------------------------
异或的思路。 非常巧妙, 以后要注意。 好了。 本文先到此为止。
某整形数组中除了两个单身整数外, 其余的整数都是成对出现的, 利用C/C++代码求出这两个单身整数。 要求: 时间复杂度o(n), 空间复杂度o(1)------某公司招聘试题的更多相关文章
- Class 找出一个整形数组中的元素的最大值
目的:找出一个整形数组中的元素的最大值 以下,我们用类和对象的方法来做. #include<iostream> using namespace std; class Array_m ...
- 算法题:int 数组中 只有一个是id 只出现一次 其他都出现2次 怎么找出只出现一次的id
首先讲一个最笨的算法:时间复杂度为N 空间复杂度为N 代码如下:输出结果id=3完全正确: int[] a = new int[] { 1, 1, 2, 2, 3, 4, 4 }; Dictiona ...
- 34.数组中2个只出现一次的数字[Find two numbers which appear once]
[题目] 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字.要求时间复杂度是O(n),空间复杂度是O(1). [分析] 这是一道很新颖的关于位运算的面试题. ...
- 【剑指Offer学习】【面试题40:数组中仅仅出现一次的数字】
题目:一个整型数组里除了两个数字之外.其它的数字都出现了两次,请敲代码找出这两个仅仅出现一次的数字. 要求时间复杂度是O(n),空间复杂度是O(1). 举例说明 比如输入数组{2, 4, 3, 6, ...
- 编程算法 - 数字数组中只出现一次 代码(C)
数字数组中只出现一次 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 一个整型数组里除了两个数字以外, 其它的数字都出现了两次. 请敲代码找出这 ...
- [leetcode]215. Kth Largest Element in an Array 数组中第k大的元素
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- Leetcode26--->Remove Duplicates from Sorted Array(从排序数组中移除相同的元素)
题目: 给定一个排序数组,移除重复出现的元素,保证每个元素最终在数组中只出现一次.返回新数组的长度length; 要求:不能分配额外的一个数组使用,必须使用原地排序的思想,且空间复杂度为O(1) 举例 ...
- 【剑指offer】数组中仅仅出现一次的数字(1)
转载请注明出处:http://blog.csdn.net/ns_code/article/details/27649027 题目描写叙述: 一个整型数组里除了两个数字之外.其它的数字都出现了两次. 请 ...
- 【剑指Offer】40、数组中只出现一次的数字
题目描述: 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字.要求时间复杂度为O(n),空间复杂度为O(1). 解题思路: 这道题目相对比较难 ...
随机推荐
- ORA-16055: FAL request rejected
主库频繁报错如下: ORA-16055: FAL request rejected 解决办法: ALTER SYSTEM SET log_archive_dest_state_2='DEFER' ...
- canvas:飞机大战
最开始我们要初始化信息,我们有五个状态,游戏封面,加载状态,运行状态,游戏暂停,游戏结束 我们还需要得分score,生命life var START = 1;//初始状态 var LOADING = ...
- VB 宏+mysql解决EXCEL表格实现自动化处理
1.表格模板自动建立源码 Sub opp()Dim myPath$, myFile$, AK As WorkbookApplication.ScreenUpdating = FalsemyPath = ...
- HDU1203 I NEED A OFFER! 【贪心】
I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- Core Animation 文档翻译—附录A(Layer样貌相关属性动画)
前言 在渲染过程中,核心动画获取Layer的各种属性并以特定的顺序渲染他们.这个顺序决定了Layer的最终的样貌.本节将会阐述通过设置不同的Layer样貌相关属性对应产生的渲染结果. 注意:Mac ...
- AndroidStudio 内存泄漏分析 Memory Monitor
ok.写一段内存泄漏的code private TextView txt; @Override protected void onCreate(Bundle savedInstanceState) { ...
- ORACLE使用WITH AS和HINT MATERIALIZE优化SQL解决FILTER效率低下
在做项目的过程中,一个页面使用类似例如以下的SQL查询数据.为了保密和使用方便,我把项目中有关的表名和字段替换使用ORACLE数据库中的系统表和字段. 在我所做的项目中.类似ALL_TABLES ...
- Dcloud课程2 什么是Dcloud
Dcloud课程2 什么是Dcloud 一.总结 一句话总结:DCloud提供了一套快速开发应用的跨平台技术方案. 1.DCloud的产品架构? MUI+(H5+)+HBuilder 2.什么是MU ...
- 驱动学习2-Menuconfig与Kconfig
Menuconfig: 执行make Menuconfig命令是为了产生.config文件 Kconfig: Kconfig中的指令可以改变Menuconfig图形界面中的东西,例如假如leds控制选 ...
- [React Intl] Format Date and Time Using react-intl FormattedDate and FormattedTime
Using the react-intl FormattedDate and FormattedTime components, we’ll render a JavaScript Date into ...