[LeetCode] 136. 只出现一次的数字
2018-04-10 本文已影响0人
拉面小鱼丸
给定一个整数数组,除了某个元素外其余元素均出现两次。请找出这个只出现一次的元素。
备注:
你的算法应该是一个线性时间复杂度。 你可以不用额外空间来实现它吗?
英文
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?
JAVA
class Solution {
public int singleNumber(int[] nums) {
int num = 0;
for (int n : nums) {
num ^= n;
}
return num;
}
}
![](https://img.haomeiwen.com/i5193518/4aaf03d0d546ad31.png)