Skip to content

Commit a07146e

Browse files
authored
feat: add solutions to lc problem: No.1304 (#4700)
1 parent 98852a2 commit a07146e

File tree

6 files changed

+103
-6
lines changed

6 files changed

+103
-6
lines changed

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func sumZero(n int) []int {
127127

128128
```ts
129129
function sumZero(n: number): number[] {
130-
const ans = new Array(n).fill(0);
130+
const ans: number[] = Array(n).fill(0);
131131
for (let i = 1, j = 0; i <= n / 2; ++i) {
132132
ans[j++] = i;
133133
ans[j++] = -i;
@@ -136,6 +136,24 @@ function sumZero(n: number): number[] {
136136
}
137137
```
138138

139+
#### Rust
140+
141+
```rust
142+
impl Solution {
143+
pub fn sum_zero(n: i32) -> Vec<i32> {
144+
let mut ans = vec![0; n as usize];
145+
let mut j = 0;
146+
for i in 1..=n / 2 {
147+
ans[j] = i;
148+
j += 1;
149+
ans[j] = -i;
150+
j += 1;
151+
}
152+
ans
153+
}
154+
}
155+
```
156+
139157
<!-- tabs:end -->
140158

141159
<!-- solution:end -->
@@ -215,6 +233,21 @@ function sumZero(n: number): number[] {
215233
}
216234
```
217235

236+
#### Rust
237+
238+
```rust
239+
impl Solution {
240+
pub fn sum_zero(n: i32) -> Vec<i32> {
241+
let mut ans = vec![0; n as usize];
242+
for i in 1..n {
243+
ans[i as usize] = i;
244+
}
245+
ans[0] = -(n * (n - 1) / 2);
246+
ans
247+
}
248+
}
249+
```
250+
218251
<!-- tabs:end -->
219252

220253
<!-- solution:end -->

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

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ tags:
5757

5858
<!-- solution:start -->
5959

60-
### Solution 1
60+
### Solution 1: Construction
61+
62+
We can start from $1$ and alternately add positive and negative numbers to the result array. We repeat this process $\frac{n}{2}$ times. If $n$ is odd, we add $0$ to the result array at the end.
63+
64+
The time complexity is $O(n)$, where $n$ is the given integer. Ignoring the space used for the answer, the space complexity is $O(1)$.
6165

6266
<!-- tabs:start -->
6367

@@ -124,7 +128,7 @@ func sumZero(n int) []int {
124128

125129
```ts
126130
function sumZero(n: number): number[] {
127-
const ans = new Array(n).fill(0);
131+
const ans: number[] = Array(n).fill(0);
128132
for (let i = 1, j = 0; i <= n / 2; ++i) {
129133
ans[j++] = i;
130134
ans[j++] = -i;
@@ -133,13 +137,35 @@ function sumZero(n: number): number[] {
133137
}
134138
```
135139

140+
#### Rust
141+
142+
```rust
143+
impl Solution {
144+
pub fn sum_zero(n: i32) -> Vec<i32> {
145+
let mut ans = vec![0; n as usize];
146+
let mut j = 0;
147+
for i in 1..=n / 2 {
148+
ans[j] = i;
149+
j += 1;
150+
ans[j] = -i;
151+
j += 1;
152+
}
153+
ans
154+
}
155+
}
156+
```
157+
136158
<!-- tabs:end -->
137159

138160
<!-- solution:end -->
139161

140162
<!-- solution:start -->
141163

142-
### Solution 2
164+
### Solution 2: Construction + Mathematics
165+
166+
We can also add all integers from $1$ to $n-1$ to the result array, and finally add the opposite of the sum of the first $n-1$ integers, which is $-\frac{n(n-1)}{2}$, to the result array.
167+
168+
The time complexity is $O(n)$, where $n$ is the given integer. Ignoring the space used for the answer, the space complexity is $O(1)$.
143169

144170
<!-- tabs:start -->
145171

@@ -208,6 +234,21 @@ function sumZero(n: number): number[] {
208234
}
209235
```
210236

237+
#### Rust
238+
239+
```rust
240+
impl Solution {
241+
pub fn sum_zero(n: i32) -> Vec<i32> {
242+
let mut ans = vec![0; n as usize];
243+
for i in 1..n {
244+
ans[i as usize] = i;
245+
}
246+
ans[0] = -(n * (n - 1) / 2);
247+
ans
248+
}
249+
}
250+
```
251+
211252
<!-- tabs:end -->
212253

213254
<!-- solution:end -->
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
impl Solution {
2+
pub fn sum_zero(n: i32) -> Vec<i32> {
3+
let mut ans = vec![0; n as usize];
4+
let mut j = 0;
5+
for i in 1..=n / 2 {
6+
ans[j] = i;
7+
j += 1;
8+
ans[j] = -i;
9+
j += 1;
10+
}
11+
ans
12+
}
13+
}

solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/Solution.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function sumZero(n: number): number[] {
2-
const ans = new Array(n).fill(0);
2+
const ans: number[] = Array(n).fill(0);
33
for (let i = 1, j = 0; i <= n / 2; ++i) {
44
ans[j++] = i;
55
ans[j++] = -i;

solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/Solution2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ public int[] sumZero(int n) {
77
ans[0] = -(n * (n - 1) / 2);
88
return ans;
99
}
10-
}
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
impl Solution {
2+
pub fn sum_zero(n: i32) -> Vec<i32> {
3+
let mut ans = vec![0; n as usize];
4+
for i in 1..n {
5+
ans[i as usize] = i;
6+
}
7+
ans[0] = -(n * (n - 1) / 2);
8+
ans
9+
}
10+
}

0 commit comments

Comments
 (0)