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?
s思路: 1. 這道題遇到過! 不用內(nèi)存,那就是一直做xor運(yùn)算,相同的數(shù)xor等于0,最后只剩下the single number. 2. 這道題就是利用兩個數(shù)異或的性質(zhì),基本也就是一道數(shù)學(xué)題!
class Solution {public: int singleNumber(vector<int>& nums) { int res=0; for(int i=0;i<nums.size();i++){ res^=nums[i]; } return res; }};新聞熱點(diǎn)
疑難解答