2019-02-03

作者: ruicore | 来源:发表于2019-02-03 09:28 被阅读0次
    LeetCode 260. Single Number III.jpg

    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.

    Example:

    Input: [1,2,1,3,2,5]
    Output: [3,5]
    Note:

    The order of the result is not important. So in the above example, [5, 3] is also correct.
    Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?

    描述

    给定一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次。 找出只出现一次的那两个元素。

    示例 :

    输入: [1,2,1,3,2,5]
    输出: [3,5]
    注意:

    结果输出的顺序并不重要,对于上面的例子, [5, 3] 也是正确答案。
    你的算法应该具有线性时间复杂度。你能否仅使用常数空间复杂度来实现?

    实现

    # -*- coding: utf-8 -*-
    # @Author:             何睿
    # @Create Date:        2019-02-03 09:02:18
    # @Last Modified by:   何睿
    # @Last Modified time: 2019-02-03 09:02:18
    
    
    class Solution:
        def singleNumber(self, nums):
            """
            :type nums: List[int]
            :rtype: List[int]
            """
            res = [0, 0]
            diff = 0
            for num in nums:
                diff ^= num
            # 制造分离因子
            diff = diff & (~(diff - 1))
            for num in nums:
                if (num & diff) == 0:
                    res[0] ^= num
                else:
                    res[1] ^= num
            return res
    

    源代码文件在这里.
    ©本文首发于何睿的博客,欢迎转载,转载需保留文章来源,作者信息和本声明.

    相关文章

      网友评论

        本文标题:2019-02-03

        本文链接:https://www.haomeiwen.com/subject/qekysqtx.html