天天看點

【LeetCode】136 & 137 & 260 - Single Number I & II &III

I.

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? 

Hide Tags:   Hash Table Bit Manipulation Hide Similar Problems:   (M) Single Number II (M) Single Number III (M) Missing Number (H) Find the Duplicate Number   Solution :  Xor

class Solution {
public:
    int singleNumber(vector<int>& nums){
        int ret=0; //it must be initialized
        for(int i=0;i<nums.size();i++){
            ret=ret^nums[i];
        }
        return ret;
    }
};      

 II.

Given an array of integers, every element appears three times except for one. Find that single one.

Note:

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 

Hide Tags :  Bit Manipulation Hide Similar Problems :  (M) Single Number (M) Single Number III   Solution : 

轉載于:https://www.cnblogs.com/irun/p/4846294.html

繼續閱讀