【LeetCode】136/137/260. Single Number I/II/II 解题报告(Python)

136. Single Number

题目地址:https://leetcode.com/problems/single-number/

题目描述

Given a non-empty 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?

Example 1:

Input: [2,2,1]
Output: 1

Example 2:

Input: [4,1,2,1,2]
Output: 4

解法:对每个数做异或即为结果

class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        ans = 0
        for i in nums:
            ans ^= i
        return ans

137. Single Number II

题目地址:https://leetcode.com/problems/single-number-ii/
参考:https://blog.csdn.net/qq_17550379/article/details/83926804

题目描述

Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.

Note:

Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

Example 1:

Input: [2,2,3,2]
Output: 3

Example 2:

Input: [0,1,0,1,0,1,99]
Output: 99

解法一:对二进制每个位求和

对二进制每一位分别求和,并对三取余

  • 注:python无位数限制,把列表转数字时需注意负数情况
class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        bitSum = [0] * 32
        for i in range(len(nums)):
            bitMask = 1
            for j in range(31, -1, -1):
                bit = nums[i] & bitMask
                if bit:
                    bitSum[j] += 1
                bitMask = bitMask << 1
        res = 0
        for i in range(1, 32):
            res = res << 1
            if bitSum[0] % 3:
                res += 1 - bitSum[i] % 3
            else:
                res += bitSum[i] % 3
        if bitSum[0] % 3:
            return -(res+1)
        return res

或者对res当作正数处理,若首位为1,则 r e s = r e s − ( 1 < < 32 ) res = res - (1<<32) res=res(1<<32)

解法二:统计当前数字出现次数

one为出现一次,two为出现两次,three出现三次

class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        one, two, three = 0, 0, 0
        for i in nums:
            two |= one & i
            one ^= i
            three = one & two
            one &= (~three)
            two &= (~three)
        return one

或者只用one,two表示

class Solution:
    def singleNumber(self, nums: List[int]) -> int:
        one, two = 0, 0
        for i in nums:
            one = one ^ i & (~two)
            two = two ^ i & (~one)
        return one

260. Single Number III

题目地址:https://leetcode.com/problems/single-number-iii/
参考:https://www.jianshu.com/p/b5e4e7870259

题目描述

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?

解法:分组,位运算

假设唯一的两个数为x、y,则所有数的异或即为z=x^y,x、y不可能完全相同,所以z不为0。
也就是说a xor b的结果中至少有一个bit是1。我们从这么多的bit中挑选出一个,然后其余位置为0,那我们就构成了这样的一种mask。
现在我们就可以遍历nums然后,通过mask就可以判断nums中的那些元素的右边第一位是1(根据上面例子),我们将这些数分成一类,将右边第一位是1的数分成为另外一类,并且我们的a和b也就被分到不同的组中。这两组数字的个数不一定相同,但是最后一定是可以相互通过xor

class Solution:
    def singleNumber(self, nums: List[int]) -> List[int]:
        a = 0
        for i in nums:
            a ^= i
        b = a
        mask = 1
        while a & mask == 0:
            mask = mask << 1
        for i in nums:
            if i & mask:
                a ^= i
            else:
                b ^= i
        return [a, b]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值