[LeetCode]Single Number 异或的妙用
1、数组中仅仅有一个元素仅仅出现一次,其余出现偶数次。
利用异或中同样元素相互抵消的方式求解。
2、数组中仅仅有2个元素仅仅出现一次。其余出现偶数次。
class Solution {
public:
int singleNumber(int A[], int n) {//返回仅仅出现一次的元素
if(n==1)return A[0];
int i,sum=A[0];
for(i=1;i<n;++i)sum^=A[i];
return sum;
}
vector<int> twoSingleNumber(int A[], int n) {
int res=singleNumber(A,n);//找出全部元素异或的结果,即两个目标元素异或的结果
int pos=0,i;
while(res){//找出res为1的一位(这是两个目标元素位数不同的一位),从而可将A划分为两部分,这两部分中,同样的数字为在同一部分,由于他们每位都同样
if(res&1)break;
res>>=1;
pos++;
}
vector<int>ans(2,0);
for(i=0;i<n;++i){
if((A[i]>>pos)&1)ans[0]^=A[i];//假设是pos位为1。即为第一部分,对这部分进行异或,终于同样的数字消除掉,得到的就是第一个元素
else ans[1]^=A[i];
}
cout<<ans[0]<<" "<<ans[1]<<endl;
return ans;
}
};
[LeetCode]Single Number 异或的妙用的更多相关文章
- [算法][LeetCode]Single Number——异或运算的巧妙运用
题目要求 Given an array of integers, every element appears twice except for one. Find that single one. N ...
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [LeetCode] Single Number 单独的数字
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- LeetCode:Single Number II
题目地址:here 题目大意:一个整数数组中,只有一个数出现一次,其余数都出现3次,在O(n)时间,O(1)空间内找到这个出现一次的数 对于”只有一个数出现一次,其余数出现2次“的情况,很简单,只要把 ...
- LeetCode Single Number III
原题链接在这里:https://leetcode.com/problems/single-number-iii/ 题目: Given an array of numbers nums, in whic ...
- [leetcode]Single Number II @ Python
原题地址:http://oj.leetcode.com/problems/single-number-ii/ 题意:Given an array of integers, every element ...
- [LeetCode] Single Number II 单独的数字之二
Given an array of integers, every element appears three times except for one. Find that single one. ...
- LeetCode——Single Number II(找出数组中只出现一次的数2)
问题: Given an array of integers, every element appears three times except for one. Find that single o ...
随机推荐
- 深入了解JWT以及JWT的执行机制
1.JWT以什么样的形式存在? eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9 ...
- [NOI2014]动物园(KMP)
题意 题解 因为,一直用j=nxt[j]来遍历,可以遍历前i个字符所有相等的前后缀长度,所以有一个暴力的想法,就是对于每一个长度,开始遍历,记录长度小于i/2的相等的前后缀数量,最后累加即可. 但显然 ...
- Python开发的简单记事本
---恢复内容开始--- 主要是利用python 自带的tkinter 库 程序的基于python3.0以上 ,各个平台都可以使用包括linux ,windows , ...
- 实验二实验结论&实验总结与体会
Part1:格式化输出函数printf()和格式化输入函数scanf() ① /* <C语言程序设计教程学习指导>p118 实验内容(1) 这是一个常用格式控制符使用示例 运行程序,结合运 ...
- 2015 Multi-University Training Contest 1 hdu 5290 Bombing plan
Bombing plan Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- Leetcode--easy系列9
#198 House Robber You are a professional robber planning to rob houses along a street. Each house ha ...
- 【Mockplus视频教程】《10分钟玩转Mockplus》
地址:http://doc.mockplus.cn/?p=152
- openSessionInView的使用原理及性能分析
看到好多项目中用到了openSessionInView,这种做法无非是开发方便,能够在JSP页面中操作数据库层方面的业务. 下边说下openSessionInView的使用方法及性能问题. 使用: 1 ...
- 足球大数据:致足球怀疑论者-The Counter(s)-Reformation反教条改革
足球大数据:致足球怀疑论者-The Counter(s)-Reformation反教条改革 注:本序列文章都是本人对<The Numbers Game>书(豆瓣链接http://book. ...
- 部署zookeeper实践
1.解压zookeeper 2.环境变量设置 hadoop@namenode:~/zookeeper-3.4.6/conf$ sudo vim /etc/profile export JAVA_HOM ...