【Leetcode】Single Number、Single Number II、Single Number III

Single Number I


list操作 t i m e : O ( n 2 ) ; s p a c e : O ( n ) / O ( 1 ) time:O(n^2);space:O(n)/O(1) time:O(n2);space:O(n)/O(1)

遍历list中的每一个元素,用额外空间记录遍历的数,这个额外空间tmp初始为空,遍历时,若存在,则从tmp中删除,不存在则加入tmp,由于只有一个数只出现了一次,其他数都是两次,所以最后tmp中只剩一个元素,将其输出。


class Solution(object):
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        tmp =[]
        for num in nums:
            if num not in tmp:
                tmp.append(num)
            else:
                tmp.remove(num)
            # print(tmp)
        # print(tmp.pop())
        return tmp.pop()
S = Solution()
nums = [1,2,1,2,4]
# nums = [2,2,1]
S.singleNumber(nums)

Runtime: 1416 ms, faster than 6.47% of Python online submissions for Single Number.
Memory Usage: 13.6 MB, less than 70.01% of Python online submissions for Single Number.


位运算 t i m e : O ( n ) ; s p a c e : O ( 1 ) time:O(n);space:O(1) time:O(n);space:O(1)

这里复习一下基本的组成原理知识(模电也学过),异或^,XOR,其计算规则为相同为0不同为1.也即 1 x o r 1 = 0 , 1 x o r 0 = 1 1xor1=0,1xor 0=1 1xor1=0,1xor0=1
同或刚好相反,相同为1不同为0.

		tmp = 0 #等价于两个相同的数异或可消去
        for num in nums:
            tmp ^= num
        print(tmp)

Runtime: 64 ms, faster than 86.18% of Python online submissions for Single Number.
Memory Usage: 13.6 MB, less than 63.72% of Python online submissions for Single


t i m e : O ( n ) ; s p a c e : O ( 1 ) time:O(n);space:O(1) time:O(n);space:O(1)

nums=[a,a,b,b,c,c,d] set(nums) = a,b,c,d

	return 2*sum(set(nums)) - sum(nums)

Runtime: 64 ms, faster than 86.18% of Python online submissions for Single Number.
Memory Usage: 14 MB, less than 27.99% of Python online submissions for Single Number.


Single Number II


位运算

主要是参考了位运算,根据真值表构造逻辑表达式。

class Solution(object):
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        H,L = 0,0
        for i in range(len(nums)):
            low_tmp = (L^nums[i])&~H
            H = H&~L&~nums[i] | ~H&L&nums[i]
            L = low_tmp
        print(L)
        return L

S = Solution()
nums = [0,1,0,1,0,1,99]
S.singleNumber(nums)

Runtime: 48 ms, faster than63.15% of Python online submissions for Single Number II.
Memory Usage: 13.2 MB, less than 56.02% of Python online submissions for Single Number II.


更精简的表达

利用构造的真值表得到表达式写出上述代码之后可以发现,有一个中间变量,尽管自以为已经很好了,但是可以先继续化简,因为上述的tmp的存在是为了继续计算H,换言之,H、L的计算依赖于L,但是对于H而言,可以用更新之后的L进行计算,于是可以针对H的计算更新L构造新的真值表。

        H,L = 0,0
        for i in range(len(nums)):
            # low_tmp = (L^nums[i])&~H
            # H = H&~L&~nums[i] | ~H&L&nums[i]
            # L = low_tmp

            L = (L^nums[i])&~H
            H = H&~L&~nums[i] | nums[i]&~H&~L
        #print(L)
        return L

Runtime: 48 ms, faster than 63.15% of Python online submissions for Single Number II.
Memory Usage: 13 MB, less than 91.57% of Python online submissions for Single Number II.


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.

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

比较笨的方法,与之前的一样,时间复杂度是 O ( n 2 ) O(n^2) O(n2)

class Solution(object):
    def singleNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        res = []
        for num in nums:
            if num not in res:
                res.append(num)
            else:
                res.remove(num)
       	#print(res)
        return res

S = Solution()
nums = [1,2,1,3,2,5]
S.singleNumber(nums)

Runtime: 804 ms, faster than 7.06% of Python online submissions for Single Number III.
Memory Usage: 13 MB, less than 84.38% of Python online submissions for Single Number III.

可以针对上面的解法稍微改进一下

		 dic = {}
        res = []
        for num in nums:
            if num not in dic:
                dic[num] = 1
            else:
                dic[num] = 2
        for value,time in dic.items():
            if time == 1:
                res.append(value)
        return res

Runtime: 48 ms, faster than 67.54% of Python online submissions for Single Number III.
Memory Usage: 14.9 MB, less than 5.36% of Python online submissions for Single Number III.

位运算
想不出来!!!

参考:
1.全网最佳Single Number II分析
2.如何根据真值表构建逻辑表达式


另外注意一点是:与-->&-->乘积;或-->|-->加法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值