您的位置:首页 > 理论基础 > 数据结构算法

LeetCode-136-Single Number-E

2018-02-04 00:58 525 查看
Given an array of integers, every element appears twice except for one. Find that single one.

解题思路:位操作

0 ^ N = N

N ^ N = 0

So….. if N is the single number

N1 ^ N1 ^ N2 ^ N2 ^…………..^ Nx ^ Nx ^ N

= (N1^N1) ^ (N2^N2) ^…………..^ (Nx^Nx) ^ N

= 0 ^ 0 ^ ……….^ 0 ^ N

= N

int singleNumber(vector<int>& nums) {
if(nums == null || nums.length == 0){
return 0;
}
int result = A[0];

for(int i = 1; i < A.length; i++){
result = result ^ A[i];
}
return result;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息