算法-Find that single one

Description

Given an 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?

1
2
3
4
5
6
7
8
9
10
11
12
 public class Solution {
public int singleNumber(int[] nums) {

int temp = 0;
for (int i=0;i<nums.length;i++)
{
temp = temp^nums[i];
}
return temp;
}
}

C

Solution

相异为1;找到相等的部分;为1的部分是不同的。

如:1 1 2 2 3 3 4 ,前三对异或为0,0和4异或为4。


算法-Find that single one
https://leehoward.cn/2019/10/15/算法-Find that single one/
作者
lihao
发布于
2019年10月15日
许可协议