[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 ...
随机推荐
- shell学习日志
0.shell的变量同环境变量不同,存在用户环境区. 变量赋值的方式是: variable_name = variable_value a= "hello" $a对a进行取值 关于 ...
- U盘安装CentOS 7系统
首先,本文适合新的不能再新的新手 小白出身的轩轩,由于最近正在学习Linux的centos系统,所以突发奇想,为什么不把轩轩的本机也安装一个centos系统呢,让两个系统互不干扰,想到就做到,遂开始动 ...
- Fastlane基础介绍
Fastlane是什么 Git地址: Fastlane 文档地址:Fastlane Document Fastlane是一整套的客户端CICD工具集合.Fastlane可以非常快速简单的搭建一个自动化 ...
- 【codeforces 417D】Cunning Gena
[题目链接]:http://codeforces.com/problemset/problem/417/D [题意] 有n个人共同完成m个任务; 每个人有可以完成的任务集(不一定所有任务都能完成); ...
- Linux 进程间通信(IPC)
Linux 进程间通信(IPC): Linux系统中除了进程和进程之间通信,我想大家也应该关注用户空间与内核空间是怎样通信的.例如说netlink等等. 除了传统进程间通信外像Socket通信也须要掌 ...
- Struts2学习(三)上传下载
今天记录一下利用struts2实现上传下载,借此案例说明一下struts2的开发流程. 须要注意的是struts2版本号不同非常多地方的写法是不同的.本例使用struts2.3.15 .有差别的地方文 ...
- Qt5的插件机制(1)--Qt 框架中的插件载入机制概述
概述 Qt的源代码中通过 Q<pluginType>Factory.Q<pluginType>Plugin 和 Q<pluginType> 这三个类实现了Qt的插件 ...
- 改动Android设备信息,如改动手机型号为iPhone7黄金土豪版!
首先你的手机必需要有ROOT权限,误操作有风险需慎重 请先开启手机的USB调试,防止手机改动后无法启动时导致的无法修复 1.假设你是在手机上改动,直接使用RE文件管理器,编辑/system/build ...
- mysql-计算字段
一.计算字段 存储在数据库表中的数据一般不是应用程序所需要的格式 1.如果想在一个字段中既显示公司名,又显示公司的地址,但这两个信息一般包含在不同的字段中. 2.城市.州和邮编存储在不同的列中,但邮件 ...
- UVA 11020 - Efficient Solutions(set)
UVA 11020 - Efficient Solutions 题目链接 题意:每个人有两个属性值(x, y).对于每个人(x,y)而言,当有还有一个人(x', y'),假设他们的属性值满足x' &l ...