【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_pos] = 1 , 那么可知在diff_pos这位上a 和 b 是不一样的。如果按照diff_pos这位的值

  分类可以将所有数分成两组: 1)diff_pos = 0的元素, 2)diff_pos = 1的元素。

  4.对3中的两组分别组内xor,因为其余元素都是 两两出现,那么最后就剩下a / b 了。

代码如下:

class Solution(object):
    def singleNumber(self, nums):
        xor = 0
        for num in nums:
            xor = xor^num
        diff_pos = 0
        for i in range(31):
            if(xor & (1 << i)):
                diff_pos = i
                break
        rec = [0,0]
        for num in nums:
            if(num & (1 << diff_pos)):
                rec[1] ^= num
            else:
                rec[0] ^= num
        return rec

  论文还没看,又在瞎搞了。。。

时间: 2024-08-25 06:25:59

【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 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

【一天一道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】137. Single Number II

题目: 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? 题解: The code seems tricky and hard

【LeetCode】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? 这题目的要求不仅是要求是线性时间,希望也不会使用额外的内存,那么也就是你无法运用

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 OJ 之 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 or

[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

260. Single Number III [medium] (Python)

题目链接 https://leetcode.com/problems/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