Skip to content

Commit 7b42b7e

Browse files
committed
feat: add solutions to lc problem: No.1304
No.1304.Find N Unique Integers Sum up to Zero
1 parent 30001ed commit 7b42b7e

File tree

6 files changed

+158
-80
lines changed

6 files changed

+158
-80
lines changed

solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/README.md

Lines changed: 77 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@
4141

4242
<!-- 这里可写通用的实现逻辑 -->
4343

44+
**方法一:构造**
45+
46+
我们可以从 $1$ 开始,依次将正数和负数交替放入结果数组中,一共循环 $\frac{n}{2}$ 次,如果 $n$ 为奇数,则最后再将 $0$ 放入结果数组中。
47+
48+
时间复杂度 $O(n)$,其中 $n$ 为给定的整数。忽略答案的空间消耗,空间复杂度 $O(1)$。
49+
50+
**方法二:构造 + 数学**
51+
52+
我们也可以将 $1$ 到 $n-1$ 的所有整数放入结果数组中,最后再把前 $n-1$ 个整数的和 $\frac{n(n-1)}{2}$ 的相反数放入结果数组中。
53+
54+
时间复杂度 $O(n)$,其中 $n$ 为给定的整数。忽略答案的空间消耗,空间复杂度 $O(1)$。
55+
4456
<!-- tabs:start -->
4557

4658
### **Python3**
@@ -50,13 +62,21 @@
5062
```python
5163
class Solution:
5264
def sumZero(self, n: int) -> List[int]:
53-
presum = 0
54-
res = []
55-
for i in range(1, n):
56-
res.append(i)
57-
presum += i
58-
res.append(-presum)
59-
return res
65+
ans = []
66+
for i in range(n >> 1):
67+
ans.append(i + 1)
68+
ans.append(-(i + 1))
69+
if n & 1:
70+
ans.append(0)
71+
return ans
72+
```
73+
74+
```python
75+
class Solution:
76+
def sumZero(self, n: int) -> List[int]:
77+
ans = list(range(1, n))
78+
ans.append(-sum(ans))
79+
return ans
6080
```
6181

6282
### **Java**
@@ -66,14 +86,25 @@ class Solution:
6686
```java
6787
class Solution {
6888
public int[] sumZero(int n) {
69-
int preSum = 0;
70-
int[] ret = new int[n];
89+
int[] ans = new int[n];
90+
for (int i = 1, j = 0; i <= n / 2; ++i) {
91+
ans[j++] = i;
92+
ans[j++] = -i;
93+
}
94+
return ans;
95+
}
96+
}
97+
```
98+
99+
```java
100+
class Solution {
101+
public int[] sumZero(int n) {
102+
int[] ans = new int[n];
71103
for (int i = 1; i < n; ++i) {
72-
ret[i - 1] = i;
73-
preSum += i;
104+
ans[i] = i;
74105
}
75-
ret[n - 1] = -preSum;
76-
return ret;
106+
ans[0] = -(n * (n - 1) / 2);
107+
return ans;
77108
}
78109
}
79110
```
@@ -84,14 +115,24 @@ class Solution {
84115
class Solution {
85116
public:
86117
vector<int> sumZero(int n) {
87-
int presum = 0;
88-
vector<int> res;
89-
for (int i = 1; i < n; ++i) {
90-
res.push_back(i);
91-
presum += i;
118+
vector<int> ans(n);
119+
for (int i = 1, j = 0; i <= n / 2; ++i) {
120+
ans[j++] = i;
121+
ans[j++] = -i;
92122
}
93-
res.push_back(-presum);
94-
return res;
123+
return ans;
124+
}
125+
};
126+
```
127+
128+
```cpp
129+
class Solution {
130+
public:
131+
vector<int> sumZero(int n) {
132+
vector<int> ans(n);
133+
iota(ans.begin(), ans.end(), 1);
134+
ans[n - 1] = -(n - 1) * n / 2;
135+
return ans;
95136
}
96137
};
97138
```
@@ -100,14 +141,24 @@ public:
100141

101142
```go
102143
func sumZero(n int) []int {
103-
presum := 0
104-
var res []int
144+
ans := make([]int, n)
145+
for i, j := 1, 0; i <= n/2; i, j = i+1, j+1 {
146+
ans[j] = i
147+
j++
148+
ans[j] = -i
149+
}
150+
return ans
151+
}
152+
```
153+
154+
```go
155+
func sumZero(n int) []int {
156+
ans := make([]int, n)
105157
for i := 1; i < n; i++ {
106-
res = append(res, i)
107-
presum += i
158+
ans[i] = i
108159
}
109-
res = append(res, -presum)
110-
return res
160+
ans[0] = -n * (n - 1) / 2
161+
return ans
111162
}
112163
```
113164

solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/README_EN.md

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,47 @@
4545
```python
4646
class Solution:
4747
def sumZero(self, n: int) -> List[int]:
48-
presum = 0
49-
res = []
50-
for i in range(1, n):
51-
res.append(i)
52-
presum += i
53-
res.append(-presum)
54-
return res
48+
ans = []
49+
for i in range(n >> 1):
50+
ans.append(i + 1)
51+
ans.append(-(i + 1))
52+
if n & 1:
53+
ans.append(0)
54+
return ans
55+
```
56+
57+
```python
58+
class Solution:
59+
def sumZero(self, n: int) -> List[int]:
60+
ans = list(range(1, n))
61+
ans.append(-sum(ans))
62+
return ans
5563
```
5664

5765
### **Java**
5866

5967
```java
6068
class Solution {
6169
public int[] sumZero(int n) {
62-
int preSum = 0;
63-
int[] ret = new int[n];
70+
int[] ans = new int[n];
71+
for (int i = 1, j = 0; i <= n / 2; ++i) {
72+
ans[j++] = i;
73+
ans[j++] = -i;
74+
}
75+
return ans;
76+
}
77+
}
78+
```
79+
80+
```java
81+
class Solution {
82+
public int[] sumZero(int n) {
83+
int[] ans = new int[n];
6484
for (int i = 1; i < n; ++i) {
65-
ret[i - 1] = i;
66-
preSum += i;
85+
ans[i] = i;
6786
}
68-
ret[n - 1] = -preSum;
69-
return ret;
87+
ans[0] = -(n * (n - 1) / 2);
88+
return ans;
7089
}
7190
}
7291
```
@@ -77,14 +96,24 @@ class Solution {
7796
class Solution {
7897
public:
7998
vector<int> sumZero(int n) {
80-
int presum = 0;
81-
vector<int> res;
82-
for (int i = 1; i < n; ++i) {
83-
res.push_back(i);
84-
presum += i;
99+
vector<int> ans(n);
100+
for (int i = 1, j = 0; i <= n / 2; ++i) {
101+
ans[j++] = i;
102+
ans[j++] = -i;
85103
}
86-
res.push_back(-presum);
87-
return res;
104+
return ans;
105+
}
106+
};
107+
```
108+
109+
```cpp
110+
class Solution {
111+
public:
112+
vector<int> sumZero(int n) {
113+
vector<int> ans(n);
114+
iota(ans.begin(), ans.end(), 1);
115+
ans[n - 1] = -(n - 1) * n / 2;
116+
return ans;
88117
}
89118
};
90119
```
@@ -93,14 +122,24 @@ public:
93122

94123
```go
95124
func sumZero(n int) []int {
96-
presum := 0
97-
var res []int
125+
ans := make([]int, n)
126+
for i, j := 1, 0; i <= n/2; i, j = i+1, j+1 {
127+
ans[j] = i
128+
j++
129+
ans[j] = -i
130+
}
131+
return ans
132+
}
133+
```
134+
135+
```go
136+
func sumZero(n int) []int {
137+
ans := make([]int, n)
98138
for i := 1; i < n; i++ {
99-
res = append(res, i)
100-
presum += i
139+
ans[i] = i
101140
}
102-
res = append(res, -presum)
103-
return res
141+
ans[0] = -n * (n - 1) / 2
142+
return ans
104143
}
105144
```
106145

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
class Solution {
22
public:
33
vector<int> sumZero(int n) {
4-
int presum = 0;
5-
vector<int> res;
6-
for (int i = 1; i < n; ++i) {
7-
res.push_back(i);
8-
presum += i;
9-
}
10-
res.push_back(-presum);
11-
return res;
4+
vector<int> ans(n);
5+
iota(ans.begin(), ans.end(), 1);
6+
ans[n - 1] = -(n - 1) * n / 2;
7+
return ans;
128
}
139
};
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
func sumZero(n int) []int {
2-
presum := 0
3-
var res []int
2+
ans := make([]int, n)
43
for i := 1; i < n; i++ {
5-
res = append(res, i)
6-
presum += i
4+
ans[i] = i
75
}
8-
res = append(res, -presum)
9-
return res
6+
ans[0] = -n * (n - 1) / 2
7+
return ans
108
}
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
class Solution {
22
public int[] sumZero(int n) {
3-
int preSum = 0;
4-
int[] ret = new int[n];
3+
int[] ans = new int[n];
54
for (int i = 1; i < n; ++i) {
6-
ret[i - 1] = i;
7-
preSum += i;
5+
ans[i] = i;
86
}
9-
ret[n - 1] = -preSum;
10-
return ret;
7+
ans[0] = -(n * (n - 1) / 2);
8+
return ans;
119
}
12-
}
10+
}
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
class Solution:
22
def sumZero(self, n: int) -> List[int]:
3-
presum = 0
4-
res = []
5-
for i in range(1, n):
6-
res.append(i)
7-
presum += i
8-
res.append(-presum)
9-
return res
3+
ans = list(range(1, n))
4+
ans.append(-sum(ans))
5+
return ans

0 commit comments

Comments
 (0)