目录
  1. 1. Description
  2. 2. Solution
算法-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
 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;
}
}

Solution

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

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

文章作者: 李浩
文章链接: https://leehoward.cn/2019/10/15/算法-Find that single one/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 leehoward
打赏
  • 微信
  • 支付宝

评论