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 than
63.15%
of Python online submissions for Single Number II.
Memory Usage: 13.2 MB, less than56.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 than91.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.如何根据真值表构建逻辑表达式
另外注意一点是:
与-->&-->乘积;或-->|-->加法