forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
37 lines (37 loc) · 1.12 KB
/
Solution.java
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
class Solution {
public int[] recoverArray(int[] nums) {
Arrays.sort(nums);
for (int i = 1, n = nums.length; i < n; ++i) {
int d = nums[i] - nums[0];
if (d == 0 || d % 2 == 1) {
continue;
}
boolean[] vis = new boolean[n];
vis[i] = true;
List<Integer> t = new ArrayList<>();
t.add((nums[0] + nums[i]) >> 1);
for (int l = 1, r = i + 1; r < n; ++l, ++r) {
while (l < n && vis[l]) {
++l;
}
while (r < n && nums[r] - nums[l] < d) {
++r;
}
if (r == n || nums[r] - nums[l] > d) {
break;
}
vis[r] = true;
t.add((nums[l] + nums[r]) >> 1);
}
if (t.size() == (n >> 1)) {
int[] ans = new int[t.size()];
int idx = 0;
for (int e : t) {
ans[idx++] = e;
}
return ans;
}
}
return null;
}
}