From 28998bfde15dcc0083cb5910b954d19c6b3f7f77 Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Sun, 7 Sep 2025 08:23:08 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1304 --- .../README.md | 35 +++++++++++++- .../README_EN.md | 47 +++++++++++++++++-- .../Solution.rs | 13 +++++ .../Solution.ts | 2 +- .../Solution2.java | 2 +- .../Solution2.rs | 10 ++++ 6 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/Solution.rs create mode 100644 solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/Solution2.rs diff --git a/solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/README.md b/solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/README.md index ee0aad236f00a..f71c1b7321906 100644 --- a/solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/README.md +++ b/solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/README.md @@ -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; @@ -136,6 +136,24 @@ function sumZero(n: number): number[] { } ``` +#### Rust + +```rust +impl Solution { + pub fn sum_zero(n: i32) -> Vec { + 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 + } +} +``` + @@ -215,6 +233,21 @@ function sumZero(n: number): number[] { } ``` +#### Rust + +```rust +impl Solution { + pub fn sum_zero(n: i32) -> Vec { + 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 + } +} +``` + diff --git a/solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/README_EN.md b/solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/README_EN.md index 759e6722c6f8e..dbe05c07c381c 100644 --- a/solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/README_EN.md +++ b/solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/README_EN.md @@ -57,7 +57,11 @@ tags: -### 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)$. @@ -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; @@ -133,13 +137,35 @@ function sumZero(n: number): number[] { } ``` +#### Rust + +```rust +impl Solution { + pub fn sum_zero(n: i32) -> Vec { + 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 + } +} +``` + -### 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)$. @@ -208,6 +234,21 @@ function sumZero(n: number): number[] { } ``` +#### Rust + +```rust +impl Solution { + pub fn sum_zero(n: i32) -> Vec { + 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 + } +} +``` + diff --git a/solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/Solution.rs b/solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/Solution.rs new file mode 100644 index 0000000000000..c42d7494944c8 --- /dev/null +++ b/solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/Solution.rs @@ -0,0 +1,13 @@ +impl Solution { + pub fn sum_zero(n: i32) -> Vec { + 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 + } +} diff --git a/solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/Solution.ts b/solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/Solution.ts index 823566474fec6..809dbe06ce47d 100644 --- a/solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/Solution.ts +++ b/solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/Solution.ts @@ -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; diff --git a/solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/Solution2.java b/solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/Solution2.java index 6edc7c1b7c1c7..26b619d2a0f01 100644 --- a/solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/Solution2.java +++ b/solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/Solution2.java @@ -7,4 +7,4 @@ public int[] sumZero(int n) { ans[0] = -(n * (n - 1) / 2); return ans; } -} \ No newline at end of file +} diff --git a/solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/Solution2.rs b/solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/Solution2.rs new file mode 100644 index 0000000000000..cce5a76f891c2 --- /dev/null +++ b/solution/1300-1399/1304.Find N Unique Integers Sum up to Zero/Solution2.rs @@ -0,0 +1,10 @@ +impl Solution { + pub fn sum_zero(n: i32) -> Vec { + 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 + } +}