码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] 260. Single Number III 单独数 III

时间:2018-09-20 15:57:46      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:his   ice   control   etc   appear   other   title   algo   using   

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.

Example:

Input:  [1,2,1,3,2,5]
Output: [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?

这次的问题变成special的数又两个了,实际上题目并不是找single number了,而是找special two number。所以之前两个题目的解法是不适用的,起码不是直接适用的。如果可以把two number的问题变成两个single number的问题,就可以套用之前第一个问题的解法了,也就是说我们可以通过某种方式把数组分为两组,每组只包含那两个special number中的一个。

问题的关键就变成如何分组了。思路也是有点巧妙,考虑到两个special number是不一样的,而恰好其余的数都是出现两次,所以如果对每个数都做亦或操作,最后的结果就是那两个special number的亦或,而且至少有一个位是1,那么就可以根据其中一个为1的位将所有的数分为两组,再套用第一个题的方法即可。

Java:

public class Solution {
    public int[] singleNumber(int[] nums) {
        // Pass 1 : 
        // Get the XOR of the two numbers we need to find
        int diff = 0;
        for (int num : nums) {
            diff ^= num;
        }
        // Get its last set bit
        diff &= -diff;
        
        // Pass 2 :
        int[] rets = {0, 0}; // this array stores the two numbers we will return
        for (int num : nums)
        {
            if ((num & diff) == 0) // the bit is not set
            {
                rets[0] ^= num;
            }
            else // the bit is set
            {
                rets[1] ^= num;
            }
        }
        return rets;
    }
}  

Python:

class Solution(object):
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        xor = 0
        a = 0
        b = 0
        for num in nums:
            xor ^= num
        mask = 1
        while(xor&mask == 0):
            mask = mask << 1
        for num in nums:
            if num&mask:
                a ^= num
            else:
                b ^= num
        return [a, b]

C++:

class Solution
{
public:
    vector<int> singleNumber(vector<int>& nums) 
    {
        // Pass 1 : 
        // Get the XOR of the two numbers we need to find
        int diff = accumulate(nums.begin(), nums.end(), 0, bit_xor<int>());
        // Get its last set bit
        diff &= -diff;

        // Pass 2 :
        vector<int> rets = {0, 0}; // this vector stores the two numbers we will return
        for (int num : nums)
        {
            if ((num & diff) == 0) // the bit is not set
            {
                rets[0] ^= num;
            }
            else // the bit is set
            {
                rets[1] ^= num;
            }
        }
        return rets;
    }
};  

C++:

class Solution {
public:
    vector<int> singleNumber(vector<int>& nums) {
        int diff = accumulate(nums.begin(), nums.end(), 0, bit_xor<int>());
        diff &= -diff;
        vector<int> res(2, 0);
        for (auto &a : nums) {
            if (a & diff) res[0] ^= a;
            else res[1] ^= a;
        }
        return res;
    }
};

  

 

类似题目:

[LeetCode] 136. Single Number 单独数

[LeetCode] 137. Single Number II 单独数 II

 

[LeetCode] 260. Single Number III 单独数 III

标签:his   ice   control   etc   appear   other   title   algo   using   

原文地址:https://www.cnblogs.com/lightwindy/p/9680951.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!