[LeetCode] 260. Single Number III(位运算)

传送门

Description

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?

思路

题意:给定一个数组,其中除了两个数出现一次外,其他都出现两次,要求在时间复杂度为O(n)空间复杂度为O(1)的条件下找出这两个数。

题解:按照异或的思想,最后可以得到这两个出现一次的数的异或值,那么我们只需对这个异或值lowbit操作,得到其最低位为1的位在哪,然后根据lowbit后得到的值,将数据分为两堆,那么可以知道的是这两个出现一次的数肯定分布在不同的两堆中(因为是对这这两个数的异或值lowbit操作,因此其为1的最低二进制位,这两个数肯定一个数0一个是1),那么问题转化为子问题,有一堆数,出了一个数出现一次外,其他数都出现两次。

class Solution {
public:
    //26ms
    vector<int> singleNumber(vector<int>& nums) {
        int diff = 0,one = 0,two = 0;
        vector<int>res;
        for (unsigned int i = 0;i < nums.size();i++){
            diff ^= nums[i];
        }
        diff &= (-diff);  //lowbit操作
        for (unsigned int i = 0;i < nums.size();i++){
            if (nums[i] & diff) one ^= nums[i];
            else    two ^= nums[i];
        }
        res.push_back(one);
        res.push_back(two);
        return res;
    }
};

  

时间: 2024-10-12 17:18:50

[LeetCode] 260. Single Number III(位运算)的相关文章

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

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: The order of the result is

Leetcode 260 Single Number III 亦或

在一个数组中找出两个不同的仅出现一次的数(其他数字出现两次) 同样用亦或来解决(参考编程之美的1.5) 先去取出总亦或值 然后分类,在最后一位出现1的数位上分类成 ans[0]和ans[1] a&(-a)就是计算出这个数,可以参考树状数组. 最后就是注意(nums[i] & a) == 0要加().注意符号运算优先级 1 class Solution { 2 public: 3 vector<int> singleNumber(vector<int>& nu

Leetcode 260. Single Number III JAVA语言

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 

260. Single Number III

260. Single Number III DescriptionHintsSubmissionsDiscussSolution DiscussPick One 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 o

136. Single Number &amp;&amp; 137. Single Number II &amp;&amp; 260. Single Number III

136. Single Number 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 co

[LeetCode][JavaScript]Single Number III

Single Number III 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]

【一天一道LeetCode】#260. Single Number III

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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 a

【LeetCode】-- 260. Single Number III

问题描述: https://leetcode.com/problems/single-number-iii/ 在一个数组里面,只有两个元素仅出现过1次,其余都出现过两次.找出出现仅一次的那两个(a, b). 要求常量空间,线性时间. 问题解决: 这题用到“神奇的位运算”. 1.因为除了特殊的两个元素,其余两两出现,那么就想到了XOR(异或). 2.从头到尾XOR之后,会得到a xor b 的结果.接下来我们试着把这两个元素分离开来. 3.我们在a xor b 中找到任意一位XOR[diff_po

【leetcode】260. Single Number III

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