标签:
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?
int singleNumber(vector<int>& nums) { if (nums.size()==1) return nums[0]; int flag=0; while (nums.size()>1) { flag=0; int temp=nums[0]; for(vector<int>::iterator iter=nums.begin()+1;iter!=nums.end();iter++) { if(*iter==temp) { nums.erase(iter); nums.erase(nums.begin()); flag=1; break; } } if (!flag) { return nums[0]; } } }
int singleNumber(vector<int>& nums) { int res=0; for(int i=0;i<nums.size();i++) res^=nums[i]; return res; }
标签:
原文地址:http://blog.csdn.net/sinat_24520925/article/details/45576735