标签:leetcode
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?
class Solution {
public:
int singleNumber(vector<int>& nums) {
for(int i=1;i<nums.size();i++){
nums[0]^=nums[i];
}
return nums[0];
}
};
要求O(n)并且不用额外的变量。 非常巧妙的技巧。相同数取异或为0,所0和所有数的异或为本身,所以最后剩下的就是Single Number。
136. Single Number leetcode做题报告
标签:leetcode
原文地址:http://zjwzjw369.blog.51cto.com/10388875/1892102