您的位置:首页 > 其它

<LeetCode OJ>Single Number( I / II / III )【136 / 137 / 260】

2016-01-01 16:11 661 查看


136. Single Number

My Submissions

Question

Total Accepted: 105907 Total
Submissions: 221979 Difficulty: Medium

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?

Subscribe to see which companies asked this question

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

第一种方法哈希法,44ms

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        unordered_set<int> hashset;  
        for(int i=0;i<nums.size();i++)  
        {  
            //if(hashset.count(num[i]))//count统计是否出现过(然而对于hashset,这样更慢一些)  
            if (hashset.find(nums[i]) != hashset.end())//直接查找,哈希查找常数时间复杂度     
                hashset.erase(nums[i]);  
            else  
                hashset.insert(nums[i]);  
        }  
        unordered_set<int>::iterator p=hashset.begin();  
        return *p;  
    }
};


第二种方法:异或运算,20ms

//别人家的思路:
//异或,异则真,同则假。XOR (^) 
//异或的性质1:交换律a ^ b = b ^ a,性质2:0 ^ a = a。
//所有元素异或,最终结果就是出现一次的数  
class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int value = 0;
        for(int i=0; i<nums.size(); i++)
            value = value ^ nums[i];//所有元素异或,最终结果就是出现一次的数  
        return value;
    }
};


当然,map,set等方法也可以做,时间复杂度为,O(N*lg(N))


137. Single Number II

My Submissions

Question

Total Accepted: 70740 Total
Submissions: 194635 Difficulty: Medium

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?

Subscribe to see which companies asked this question

Hide Tags
Bit Manipulation

Hide Similar Problems
(M) Single Number (M)
Single Number III

我的解法(错误的解法,部分未通过)

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        unordered_set<int> hashset;    
        for(int i=0;i<nums.size();i++)    
        {    
            if(hashset.count(nums[i])==2)//count统计是否出现过(然而对于hashset,这样更慢一些)    
                hashset.erase(nums[i]);    
            else    
                hashset.insert(nums[i]);    
        }    
        unordered_set<int>::iterator p=hashset.begin();    
        return *p;    
    }
};


别人家的解法:

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int a=0;
        int b=0;
        for(int c:nums){
            int ta=(~a&b&c)|(a&~b&~c);
            b=(~a&~b&c)|(~a&b&~c);
            a=ta;
        }
        //we need find the number that is 01,10 => 1, 00 => 0.
        return a|b;
    }
};



260. Single Number III

My Submissions

Question

Total Accepted: 19802 Total
Submissions: 47891 Difficulty: Medium

Given an array of numbers
nums
, in which
exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.
For example:
Given
nums = [1, 2, 1, 3, 2, 5]
, return
[3,
 5]
.
Note:

The order of the result is not important. So in the above example,
[5, 3]
is also
correct.
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?

Credits:

Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

Hide Tags
Bit Manipulation

Hide Similar Problems
(M) Single Number (M)
Single Number II

第一种方法:set方法(红黑树),68ms

思路首先:利用set来做,如果nums[i]在set中就不插入到set并且删除该值
class Solution {
public:
    vector<int> singleNumber(vector<int>& nums) {
        set<int> s;
        vector<int> num=nums;
        for(int i=0;i<num.size();i++)
        {
            //if(s.count(num[i]))//count统计是否出现过(更快一些)
            if (s.find(num[i]) != s.end())//直接查找,红黑树查找很快   
                s.erase(num[i]);
            else
                s.insert(num[i]);
        }
        vector<int> ans(2,0);
        set<int>::iterator p;
        int i=0;
        for(p = s.begin();p != s.end();p++,i++)
            ans[i]=*p; //set是无序容器,不能像数组那样s[i]这样的操作
        return ans;
    }
};


第二种方法:哈希方法,32ms

//思路首先:利用unordered_set来做,如果nums[i]在unordered_set中就不插入到unordered_set并且删除该值
class Solution {
public:
    vector<int> singleNumber(vector<int>& nums) {
        unordered_set<int> hashset;
        vector<int> num=nums;
        for(int i=0;i<num.size();i++)
        {
            //if(hashset.count(num[i]))//count统计是否出现过(然而对于hashset,这样更慢一些)
            if (hashset.find(num[i]) != hashset.end())//直接查找,红黑树查找很快   
                hashset.erase(num[i]);
            else
                hashset.insert(num[i]);
        }
        vector<int> ans(2,0);
        unordered_set<int>::iterator p;
        int i=0;
        for(p = hashset.begin();p != hashset.end();p++,i++)
            ans[i]=*p; //set是无序容器,不能像数组那样s[i]这样的操作
        return ans;
    }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: