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