首页
外语
计算机
考研
公务员
职业资格
财经
工程
司法
医学
专升本
自考
实用职业技能
登录
计算机
一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度是O(n),空间复杂度是O(1)。
一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度是O(n),空间复杂度是O(1)。
admin
2019-03-29
84
问题
一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度是O(n),空间复杂度是O(1)。
选项
答案
/////////////////////////////////////////////////////////////////////// // Find two numbers which only appear once in an array // Input: data - an array contains two number appearing exactly once, // while others appearing exactly twice // length - the length of data // Output: num1 - the first number appearing once in data // num2 - the second number appearing once in data /////////////////////////////////////////////////////////////////////// void FindNumsAppearOnce(int data[], int length, int &num1, int &num2) { if (length < 2) return; // get num1 ^ num2 int resultExclusiveOR = 0; for (int i = 0; i < length; ++ i) resultExclusiveOR ^= data[i]; // get index of the first bit, which is 1 in resultExclusiveOR unsigned int indexOf1 = FindFirstBitIs1(resultExclusiveOR); num1 = num2 = 0; for (int j = 0; j < length; ++ j) { // divide the numbers in data into two groups, // the indexOf1 bit of numbers in the first group is 1, // while in the second group is 0 if(IsBit1(data[j], indexOf1)) num1 ^= data[j]; else num2 ^= data[j]; } } /////////////////////////////////////////////////////////////////////// // Find the index of first bit which is 1 in num (assuming not 0) /////////////////////////////////////////////////////////////////////// unsigned int FindFirstBitIs1(int num) { int indexBit = 0; while (((num & 1) == 0) && (indexBit < 32)) { num = num >> 1; ++ indexBit; } return indexBit; } /////////////////////////////////////////////////////////////////////// // Is the indexBit bit of num 1? /////////////////////////////////////////////////////////////////////// bool IsBit1(int num, unsigned int indexBit) { num = num >> indexBit; return (num & 1); }
解析
这是一道很新颖的关于位运算的面试题。
首先我们考虑这个问题的一个简单版本:一个数组里除了一个数字之外,其他的数字都出现了两次。请写程序找出这个只出现一次的数字。
这个题目的突破口在哪里?题目为什么要强调有一个数字出现一次,其他的出现两次?我们想到了异或运算的性质:任何一个数字异或它自己都等于0。也就是说,如果我们从头到尾依次异或数组中的每一个数字,那么最终的结果刚好是那个只出现依次的数字,因为那些出现两次的数字全部在异或中抵消掉了。
有了上面简单问题的解决方案之后,我们回到原始的问题。如果能够把原数组分为两个子数组。在每个子数组中,包含一个只出现一次的数字,而其他数字都出现两次。如果能够这样拆分原数组,按照前面的办法就是分别求出这两个只出现一次的数字了。
我们还是从头到尾依次异或数组中的每一个数字,那么最终得到的结果就是两个只出现一次的数字的异或结果。因为其他数字都出现了两次,在异或中全部抵消掉了。由于这两个数字肯定不一样,那么这个异或结果肯定不为0,也就是说在这个结果数字的二进制表示中至少就有一位为1。我们在结果数字中找到第一个为1的位的位置,记为第N位。现在我们以第N位是不是1为标准把原数组中的数字分成两个子数组,第一个子数组中每个数字的第N位都为1,而第二个子数组的每个数字的第N位都为0。
现在我们已经把原数组分成了两个子数组,每个子数组都包含一个只出现一次的数字,而其他数字都出现了两次。因此到此为止,所有的问题我们都已经解决。
转载请注明原文地址:https://kaotiyun.com/show/5RmZ777K
0
程序员面试
相关试题推荐
TheUnitedStatesInterstateHighwaySystemisaninfrastructurefeatofunprecedentedproportions.Notonlydoesitjoinallfi
定义栈的数据结构,要求添加一个min函数,能够得到栈的最小元素。要求函数min、push以及pop的时间复杂度都是O(1)。
输入一个链表的头结点,从尾到头反过来输出每个结点的值。链表结点定义如下:structListNode{intm_nKey;ListNode*m_pNext;};
自定义工具栏上的按钮添加“自动索引”按钮,删除“查找”按钮。
为邮件到达后应用规则“若发件人包含‘mary@sina.com’转发到wangtao@sina.com”。
邮件的删除。
在当前界面【管理工具】窗口中,设置Windows密码策略,将密码长度最小值设置为8个字符。
关于计算机语言的描述,不正确的是()。A.机器语言的语句全部由0和1组成,指令代码短,执行速度快B.机器语言因为是面向机器的低级语言,所以执行速度慢C.汇编语言已将机器语言符号化,所以它与机器无关D.汇编语言比机器语言执行速度快
下列有关break和continue语句的叙述中,正确的是________。
随机试题
在影响个体身心发展的诸多因素中,_______具有加速个体发展的特殊功能。
使用FTP服务时,可以采用匿名登录的方式。
下列不属于生物药剂学研究内容的是
证券公司经纪业务内部控制应重点防范规模失控、决策失误、超越授权、变相自营、账外自营、操纵市场、内幕交易等的风险。()
乙公司是一家家族企业,依靠家传的食品配方经营着几家连锁食品店,以前店后厂的形式进行经营。随着大规模连锁食品企业的出现,该家族企业面临较大的竞争压力。王某是该家族企业年轻一代的继承人。王某根据自己在MBA学习期间学到的知识,认为公司应该进行变革,但变革就会面
现行会计制度允许列入管理费用的税金有()。
根据以下资料,回答问题。2012年国家火炬计划项目总立项数2108项,其中产业化示范项目1834项,环境建设项目274项。2012年国家火炬计划重点支持项目445项。2012年,国家火炬计划项目中央财政安排经费3.2亿元支持重点项目。
篮球比赛中,每支球队上场球员为5名。某支篮球队共有12名球员,其中后卫5名(全明星球员1名),前锋5名(全明星球员1名),中锋2名。主教练准备排出双后卫阵型,且要保证全明星球员都要上场,总共有多少种安排方式?
A.seetheworldB.alientoC.atrandomD.liveinA.residentsinthesecommunitieswerephoned【T13】______andaskedthe
Theexperimentistotakeplace
最新回复
(
0
)