leetcode 136 Single Number bBt Option

时间:2021-11-03 16:19:38

Linked Url:https://leetcode.com/problems/single-number/

Given a non-empty array of integers, every element appears twice except for one. Find that single one.

Note:

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

Example :
Input: [,,]
Output: 1
Example :
Input: [,,,,]
Output:

solution:

The method is xor option, principles is  : 0 xor 0 = 0; 0 xor 1 = 1;any  num xor itself  =  0,so we pass the array and xor its elements all,so the result  which is we want.

Ac code follow:

 class Solution {
public:
int singleNumber(vector<int>& nums) {
int res = nums[];
for(int i = ;i < nums.size();++i)
res = nums[i]^res;
return res;
}
};