Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func sumZero(n int) []int {

```ts
function sumZero(n: number): number[] {
const ans = new Array(n).fill(0);
const ans: number[] = Array(n).fill(0);
for (let i = 1, j = 0; i <= n / 2; ++i) {
ans[j++] = i;
ans[j++] = -i;
Expand All @@ -136,6 +136,24 @@ function sumZero(n: number): number[] {
}
```

#### Rust

```rust
impl Solution {
pub fn sum_zero(n: i32) -> Vec<i32> {
let mut ans = vec![0; n as usize];
let mut j = 0;
for i in 1..=n / 2 {
ans[j] = i;
j += 1;
ans[j] = -i;
j += 1;
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down Expand Up @@ -215,6 +233,21 @@ function sumZero(n: number): number[] {
}
```

#### Rust

```rust
impl Solution {
pub fn sum_zero(n: i32) -> Vec<i32> {
let mut ans = vec![0; n as usize];
for i in 1..n {
ans[i as usize] = i;
}
ans[0] = -(n * (n - 1) / 2);
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Construction

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.

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)$.

<!-- tabs:start -->

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

```ts
function sumZero(n: number): number[] {
const ans = new Array(n).fill(0);
const ans: number[] = Array(n).fill(0);
for (let i = 1, j = 0; i <= n / 2; ++i) {
ans[j++] = i;
ans[j++] = -i;
Expand All @@ -133,13 +137,35 @@ function sumZero(n: number): number[] {
}
```

#### Rust

```rust
impl Solution {
pub fn sum_zero(n: i32) -> Vec<i32> {
let mut ans = vec![0; n as usize];
let mut j = 0;
for i in 1..=n / 2 {
ans[j] = i;
j += 1;
ans[j] = -i;
j += 1;
}
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### Solution 2
### Solution 2: Construction + Mathematics

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.

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)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -208,6 +234,21 @@ function sumZero(n: number): number[] {
}
```

#### Rust

```rust
impl Solution {
pub fn sum_zero(n: i32) -> Vec<i32> {
let mut ans = vec![0; n as usize];
for i in 1..n {
ans[i as usize] = i;
}
ans[0] = -(n * (n - 1) / 2);
ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
impl Solution {
pub fn sum_zero(n: i32) -> Vec<i32> {
let mut ans = vec![0; n as usize];
let mut j = 0;
for i in 1..=n / 2 {
ans[j] = i;
j += 1;
ans[j] = -i;
j += 1;
}
ans
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function sumZero(n: number): number[] {
const ans = new Array(n).fill(0);
const ans: number[] = Array(n).fill(0);
for (let i = 1, j = 0; i <= n / 2; ++i) {
ans[j++] = i;
ans[j++] = -i;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ public int[] sumZero(int n) {
ans[0] = -(n * (n - 1) / 2);
return ans;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
impl Solution {
pub fn sum_zero(n: i32) -> Vec<i32> {
let mut ans = vec![0; n as usize];
for i in 1..n {
ans[i as usize] = i;
}
ans[0] = -(n * (n - 1) / 2);
ans
}
}