260. Single Number III

原创 2016年05月31日 19:17:40

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?

1.我的答案 

当然是最直接的啦,用一个哈希表存储,最后来遍历一遍

class Solution {
public:
    vector<int> singleNumber(vector<int>& nums) {
        map<int,int> mp;
        vector<int> res;
        int len = nums.size();
        for(int i = 0; i < len; i++){
            if(mp.find(nums[i]) == mp.end()){
                mp[nums[i]] = 1;
            }
            else
                mp[nums[i]]++;
        }
        for(auto iter = mp.begin(); iter != mp.end(); iter++){
            if(iter->second == 1)
            res.push_back(iter->first);
        }
        return res;
    }
};

2.看了别人答案做的

思路:先把所有的值都异或一遍,相同的值异或异或肯定是0,则剩下的值就是单个出现的这两个数的异或结果。此时找该数的最右边(最左边也一样)第一次出现的1 的位置(该位置对应的那两个数一定是0和1),用此位置将该数组分为两部分。一部分是此位置为1的数,另一部分是此位置为0的数。同时这两边数各自含有一个唯一出现一次的数,其余都是出现两次的数。然后找出这个只出现一次的数即可。

这里有个小技巧:

要得到最右边第一次出现1的位置为1的数,可用以下方法:

int bit = a & ~(a-1);

bit即可得到a中右边第一次出现1的数。

例如: a = 110, 则bit = 110 & 010 = 010

以下是代码部分:

class Solution {
public:
    vector<int> singleNumber(vector<int>& nums) {
        int temp = 0;
        for(int i = 0; i < nums.size(); i++){
            temp ^= nums[i];
        }
        int xors = temp & ~(temp - 1);
        int num1 = 0, num2 = 0;
        for(int j = 0; j < nums.size(); j++){
            if((nums[j] & xors) > 0)
                num1 ^= nums[j];
            else
                num2 ^= nums[j];
        }
        vector<int> res;
        res.push_back(num1);
        res.push_back(num2);
        return res;
    }
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

260. Single Number III

Given an array of numbers nums, in which exactly two elements appear only once and all the other e...

Leetcode260 Single Number III

leetcode260

260. Single Number III(C++)

题目: Given an array of numbers nums, in which exactly two elements appear only once and all the oth...

463. Island Perimeter\242. Valid Anagram\*260. Single Number III\409. Longest Palindrome

Island Perimeter 题目描述 代码实现463. Island Perimeter题目描述You are given a map in form of a two-dimensional ...

leetcode 260.Single Number III

在leetcode的第260题 Single Number III 中有位大神的解题方法。

LeetCode(260) Single Number III

题目Given an array of numbers nums, in which exactly two elements appear only once and all the other e...
  • fly_yr
  • fly_yr
  • 2015-12-02 14:41
  • 1006

LeetCode笔记:260. Single Number III

限制条件下找出数组中唯一的两个数字

LeetCode oj 260. Single Number III (位运算)

260. Single Number III   Question Editorial Solution  My Submissions Total Ac...

[leetcode] 260.Single Number III

题目: Given an array of numbers nums, in which exactly two elements appear only once and all the othe...

LeetCode 260. Single Number III 题解(C++)

Given an array of numbers nums, in which exactly two elements appear only once and all the other ele...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)