[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 ...
随机推荐
- Remember the Word UVALive - 3942 DP_字典树
每个小单词的长度都是小于等于100的,这是个重要的突破口. Code: #include <cstdio> #include <algorithm> #include < ...
- 路飞学城Python-Day15(模块二思维导图)
- yii2.0 发送邮件带word小附件
把 common/config/main-local.php 下的 mailer 注释掉: 'mailer'=>[ 'class ...
- 【jQuery02】点击标题面板显示内容
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【转】Java集合间的相互转换
下面代码演示了List<-->数组.List<-->Set.数组<-->Set.Map将键转化为Set.Map将值转化为Set.Map将值转化为List等集合常用转 ...
- android将String转化为MD5的方法+一些String经常使用的方法
public class StringUtils { public static String MD5Encode(String origin) { String resultString = nul ...
- Cocos2dx 小技巧(十五)话说ScrollView的delegate实现过程
附:本文參加了CSDN博客大赛.亲假设认为这篇文章不错,就大胆的来投上一票吧! !!http://vote.blog.csdn.net/Article/Details? articleid=34140 ...
- swift入门-实现简单的登录界面
// // AppDelegate.swift // UIWindow import UIKit @UIApplicationMain class AppDelegate: UIResponder, ...
- 深度学习系列之ANN
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd3F0aGFoYQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA ...
- zzulioj--1775-- 和尚特烦恼1——是不是素数(素数水题)
1775: 和尚特烦恼1--是不是素数 Time Limit: 2 Sec Memory Limit: 128 MB Submit: 563 Solved: 193 SubmitStatusWeb ...