先看看这个题目:某整形数组中除了两个单身整数外, 其余的整数都是成对出现的, 利用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)------某公司招聘试题的更多相关文章

  1. Class 找出一个整形数组中的元素的最大值

    目的:找出一个整形数组中的元素的最大值   以下,我们用类和对象的方法来做.   #include<iostream> using namespace std; class Array_m ...

  2. 算法题:int 数组中 只有一个是id 只出现一次 其他都出现2次 怎么找出只出现一次的id

    首先讲一个最笨的算法:时间复杂度为N  空间复杂度为N 代码如下:输出结果id=3完全正确: int[] a = new int[] { 1, 1, 2, 2, 3, 4, 4 }; Dictiona ...

  3. 34.数组中2个只出现一次的数字[Find two numbers which appear once]

    [题目] 一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字.要求时间复杂度是O(n),空间复杂度是O(1). [分析] 这是一道很新颖的关于位运算的面试题. ...

  4. 【剑指Offer学习】【面试题40:数组中仅仅出现一次的数字】

    题目:一个整型数组里除了两个数字之外.其它的数字都出现了两次,请敲代码找出这两个仅仅出现一次的数字. 要求时间复杂度是O(n),空间复杂度是O(1). 举例说明 比如输入数组{2, 4, 3, 6, ...

  5. 编程算法 - 数字数组中只出现一次 代码(C)

    数字数组中只出现一次 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 一个整型数组里除了两个数字以外, 其它的数字都出现了两次. 请敲代码找出这 ...

  6. [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 ...

  7. Leetcode26--->Remove Duplicates from Sorted Array(从排序数组中移除相同的元素)

    题目: 给定一个排序数组,移除重复出现的元素,保证每个元素最终在数组中只出现一次.返回新数组的长度length; 要求:不能分配额外的一个数组使用,必须使用原地排序的思想,且空间复杂度为O(1) 举例 ...

  8. 【剑指offer】数组中仅仅出现一次的数字(1)

    转载请注明出处:http://blog.csdn.net/ns_code/article/details/27649027 题目描写叙述: 一个整型数组里除了两个数字之外.其它的数字都出现了两次. 请 ...

  9. 【剑指Offer】40、数组中只出现一次的数字

      题目描述:   一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字.要求时间复杂度为O(n),空间复杂度为O(1).   解题思路:   这道题目相对比较难 ...

随机推荐

  1. Angularjs: 封装layDate指令

    [摘要]由于业务需要,将bootstrap-datetimepicker改成了layDate. layDate是一个较成熟且便于操作的jQuery日期插件,支持同一个视图内范围选择.封装成一个指令在多 ...

  2. redirect_uri 参数错误

    http://www.cnblogs.com/zitjubiz/p/5935712.html http://blog.csdn.net/u014033756/article/details/52038 ...

  3. VBS 脚本调用

    https://my.oschina.net/Tsybius2014/blog/602641

  4. listview-属性大全

    <ListView <!-- 决定listview里的内容是否从底部开始 -- android:stackFromBottom="true" </Listview ...

  5. Spring MVC handler interceptors example--转载

    原文地址:http://www.mkyong.com/spring-mvc/spring-mvc-handler-interceptors-example/ Spring MVC allow you ...

  6. 网络博客 VC\图案像处理

    http://blog.csdn.net/lvwx369/article/category/1185452 http://blog.csdn.net/lyy289065406/article/deta ...

  7. 在线java反编译服务

    大家是否遇到过有java class文件,却没有java源码的苦恼.近期findmaven.net提供了在线java反编译服务http://www.findmaven.net/decompile_cn ...

  8. GCJ 2009 Round 2 Problem A. Crazy Rows

    https://code.google.com/codejam/contest/204113/dashboard 题目大意: 给你一个矩阵,让你转化为下三角矩阵,每次只能交换相邻的行,求最小的交换次数 ...

  9. 8、for 、emumrate、range、if

    1.for循环用户按照顺序循环可迭代对象中的内容,PS:break.continueli = [11,22,33,44]for item in li: print item 2.enumrate 为可 ...

  10. PythonOOP面向对象编程2

    编程语言的特征: 继承 封装 多态 如:C++ / Java / Python / Swift / C# inheritance 继承 drived 派生 概念: 继承是指从已有的类中衍生出新类,新类 ...