|
57 | 57 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
58 | 58 |
|
59 | 59 | ```python
|
60 |
| - |
| 60 | +class Solution: |
| 61 | + def findOriginalArray(self, changed: List[int]) -> List[int]: |
| 62 | + if len(changed) % 2 != 0: |
| 63 | + return [] |
| 64 | + n = 100010 |
| 65 | + counter = [0] * n |
| 66 | + for x in changed: |
| 67 | + counter[x] += 1 |
| 68 | + if counter[0] % 2 != 0: |
| 69 | + return [] |
| 70 | + res = [0] * (counter[0] // 2) |
| 71 | + for i in range(1, n): |
| 72 | + if counter[i] == 0: |
| 73 | + continue |
| 74 | + if i * 2 > n or counter[i] > counter[i*2]: |
| 75 | + return [] |
| 76 | + res.extend([i] * counter[i]) |
| 77 | + counter[i*2] -= counter[i] |
| 78 | + return res |
61 | 79 | ```
|
62 | 80 |
|
63 | 81 | ### **Java**
|
64 | 82 |
|
65 | 83 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
66 | 84 |
|
67 | 85 | ```java
|
| 86 | +class Solution { |
| 87 | + public int[] findOriginalArray(int[] changed) { |
| 88 | + if (changed.length % 2 != 0) { |
| 89 | + return new int[]{}; |
| 90 | + } |
| 91 | + int n = 100010; |
| 92 | + int[] counter = new int[n]; |
| 93 | + for (int x : changed) { |
| 94 | + ++counter[x]; |
| 95 | + } |
| 96 | + if (counter[0] % 2 != 0) { |
| 97 | + return new int[]{}; |
| 98 | + } |
| 99 | + int[] res = new int[changed.length / 2]; |
| 100 | + int j = counter[0] / 2; |
| 101 | + for (int i = 1; i < n; ++i) { |
| 102 | + if (counter[i] == 0) { |
| 103 | + continue; |
| 104 | + } |
| 105 | + if (i * 2 >= n || counter[i] > counter[i * 2]) { |
| 106 | + return new int[]{}; |
| 107 | + } |
| 108 | + counter[i * 2] -= counter[i]; |
| 109 | + while (counter[i]-- > 0) { |
| 110 | + res[j++] = i; |
| 111 | + } |
| 112 | + } |
| 113 | + return res; |
| 114 | + } |
| 115 | +} |
| 116 | +``` |
| 117 | + |
| 118 | +### **C++** |
| 119 | + |
| 120 | +```cpp |
| 121 | +class Solution { |
| 122 | +public: |
| 123 | + vector<int> findOriginalArray(vector<int>& changed) { |
| 124 | + if (changed.size() % 2 != 0) return {}; |
| 125 | + int n = 100010; |
| 126 | + vector<int> counter(n); |
| 127 | + for (int x : changed) ++counter[x]; |
| 128 | + if (counter[0] % 2 != 0) return {}; |
| 129 | + vector<int> res(changed.size() / 2); |
| 130 | + int j = counter[0] / 2; |
| 131 | + for (int i = 1; i < n; ++i) |
| 132 | + { |
| 133 | + if (counter[i] == 0) continue; |
| 134 | + if (i * 2 >= n || counter[i] > counter[i * 2]) return {}; |
| 135 | + counter[i * 2] -= counter[i]; |
| 136 | + while (counter[i]--) res[j++] = i; |
| 137 | + } |
| 138 | + return res; |
| 139 | + } |
| 140 | +}; |
| 141 | +``` |
68 | 142 |
|
| 143 | +### **Go** |
| 144 | +
|
| 145 | +```go |
| 146 | +func findOriginalArray(changed []int) []int { |
| 147 | + if len(changed)%2 != 0 { |
| 148 | + return []int{} |
| 149 | + } |
| 150 | + n := 100010 |
| 151 | + counter := make([]int, n) |
| 152 | + for _, x := range changed { |
| 153 | + counter[x]++ |
| 154 | + } |
| 155 | + if counter[0]%2 != 0 { |
| 156 | + return []int{} |
| 157 | + } |
| 158 | + var res []int |
| 159 | + for j := 0; j < counter[0]/2; j++ { |
| 160 | + res = append(res, 0) |
| 161 | + } |
| 162 | + for i := 1; i < n; i++ { |
| 163 | + if counter[i] == 0 { |
| 164 | + continue |
| 165 | + } |
| 166 | + if i*2 >= n || counter[i] > counter[i*2] { |
| 167 | + return []int{} |
| 168 | + } |
| 169 | + for j := 0; j < counter[i]; j++ { |
| 170 | + res = append(res, i) |
| 171 | + } |
| 172 | + counter[i*2] -= counter[i] |
| 173 | + } |
| 174 | + return res |
| 175 | +} |
69 | 176 | ```
|
70 | 177 |
|
71 | 178 | ### **...**
|
|
0 commit comments