forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.py
25 lines (25 loc) · 824 Bytes
/
Solution.py
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
class Solution:
def recoverArray(self, nums: List[int]) -> List[int]:
nums.sort()
n = len(nums)
for i in range(1, n):
d = nums[i] - nums[0]
if d == 0 or d % 2 == 1:
continue
vis = [False] * n
vis[i] = True
ans = [(nums[0] + nums[i]) >> 1]
l, r = 1, i + 1
while r < n:
while l < n and vis[l]:
l += 1
while r < n and nums[r] - nums[l] < d:
r += 1
if r == n or nums[r] - nums[l] > d:
break
vis[r] = True
ans.append((nums[l] + nums[r]) >> 1)
l, r = l + 1, r + 1
if len(ans) == (n >> 1):
return ans
return []