forked from LittleLory/codePool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathn268.py
More file actions
40 lines (36 loc) · 917 Bytes
/
n268.py
File metadata and controls
40 lines (36 loc) · 917 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
268. Missing Number
"""
class Solution(object):
"""
位操作,骚操作,没想明白这个理论依据是什么。。
missing =4∧(0∧0)∧(1∧1)∧(2∧3)∧(3∧4)
=(4∧4)∧(0∧0)∧(1∧1)∧(3∧3)∧2
=0∧0∧0∧0∧2
=2
"""
def missingNumber_1(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
missing = len(nums)
for i in range(len(nums)):
missing ^= i ^ nums[i]
return missing
"""
用等差数列求和公式
"""
def missingNumber_2(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
length = len(nums)
expect = length * (length + 1) / 2
actual = 0
for i in range(length):
actual += nums[i]
return expect - actual