【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? 

 
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
posted @ 2015-09-29 14:40  irun  阅读(120)  评论(0编辑  收藏  举报