136/137/260. Single Number( I / II / III )

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;  
    }
};


(参考于讨论区)

第二种方法:

异或,异则真,同则假。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



我的解法(没有符合题目要求),28ms

class Solution {
public:
	int singleNumber(vector<int>& nums) {
		unordered_multiset<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_multiset<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:

  1. The order of the result is not important. So in the above example, [5, 3] is also correct.
  2. 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;
    }
};


注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50507131

原作者博客:http://blog.csdn.net/ebowtang



你可能感兴趣的:(LeetCode,数据结构,算法,面试)