您的位置:首页 > 编程语言 > Go语言

Single Number

2014-01-28 00:23 381 查看


Single Number

 Total Accepted: 10122 Total
Submissions: 22355My Submissions

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?

Discuss

XOR 位操作,因为出现两次,所有XOR两次正好抵消,最后剩下只出现一次的那个数。

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