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 1:
Input: [2,2,1]
Output: 1Example 2:Input: [4,1,2,1,2]
Output: 4
题目大意:
给定一个非空数组,数组中只有一个元素只出现了一次,其他元素都出现了两次,求出现一次的元素。
理 解:
异或数组所有元素结果即为所求元素。两数相同异或为0。任何数与0异或为它本身。比较简单的一题。
代 码 C++:
class Solution {public: int singleNumber(vector & nums) { int res=nums[0]; for(int i=1;i
运行结果:
执行用时 : 16 ms 内存消耗 : 9.7 MB