diff --git a/solution/2500-2599/2573.Find the String with LCP/README.md b/solution/2500-2599/2573.Find the String with LCP/README.md index e106713fddece..68d460520f0e1 100644 --- a/solution/2500-2599/2573.Find the String with LCP/README.md +++ b/solution/2500-2599/2573.Find the String with LCP/README.md @@ -48,7 +48,7 @@ tags:
 输入:lcp = [[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,1]]
 输出:"aaaa"
-解释:lcp 对应只有一个不同字母的任意 4 字母字符串,字典序最小的是 "aaaa" 。 
+解释:lcp 对应只有一个不同字母的任意 4 字母字符串,字典序最小的是 "aaaa" 。
 

示例 3:

@@ -298,6 +298,56 @@ function findTheString(lcp: number[][]): string { } ``` +#### Rust + +```rust +impl Solution { + pub fn find_the_string(lcp: Vec>) -> String { + let n = lcp.len(); + let mut s = vec!['\0'; n]; + let mut i = 0; + + for c in b'a'..=b'z' { + while i < n && s[i] != '\0' { + i += 1; + } + if i == n { + break; + } + for j in i..n { + if lcp[i][j] > 0 { + s[j] = c as char; + } + } + } + + for i in 0..n { + if s[i] == '\0' { + return "".to_string(); + } + } + + for i in (0..n).rev() { + for j in (0..n).rev() { + if s[i] == s[j] { + if i == n - 1 || j == n - 1 { + if lcp[i][j] != 1 { + return "".to_string(); + } + } else if lcp[i][j] != lcp[i + 1][j + 1] + 1 { + return "".to_string(); + } + } else if lcp[i][j] > 0 { + return "".to_string(); + } + } + } + + s.into_iter().collect() + } +} +``` + diff --git a/solution/2500-2599/2573.Find the String with LCP/README_EN.md b/solution/2500-2599/2573.Find the String with LCP/README_EN.md index 01b2c511e62da..85a8e76ff3934 100644 --- a/solution/2500-2599/2573.Find the String with LCP/README_EN.md +++ b/solution/2500-2599/2573.Find the String with LCP/README_EN.md @@ -47,7 +47,7 @@ tags:
 Input: lcp = [[4,3,2,1],[3,3,2,1],[2,2,2,1],[1,1,1,1]]
 Output: "aaaa"
-Explanation: lcp corresponds to any 4 letter string with a single distinct letter. The lexicographically smallest of them is "aaaa". 
+Explanation: lcp corresponds to any 4 letter string with a single distinct letter. The lexicographically smallest of them is "aaaa".
 

Example 3:

@@ -296,6 +296,56 @@ function findTheString(lcp: number[][]): string { } ``` +#### Rust + +```rust +impl Solution { + pub fn find_the_string(lcp: Vec>) -> String { + let n = lcp.len(); + let mut s = vec!['\0'; n]; + let mut i = 0; + + for c in b'a'..=b'z' { + while i < n && s[i] != '\0' { + i += 1; + } + if i == n { + break; + } + for j in i..n { + if lcp[i][j] > 0 { + s[j] = c as char; + } + } + } + + for i in 0..n { + if s[i] == '\0' { + return "".to_string(); + } + } + + for i in (0..n).rev() { + for j in (0..n).rev() { + if s[i] == s[j] { + if i == n - 1 || j == n - 1 { + if lcp[i][j] != 1 { + return "".to_string(); + } + } else if lcp[i][j] != lcp[i + 1][j + 1] + 1 { + return "".to_string(); + } + } else if lcp[i][j] > 0 { + return "".to_string(); + } + } + } + + s.into_iter().collect() + } +} +``` + diff --git a/solution/2500-2599/2573.Find the String with LCP/Solution.rs b/solution/2500-2599/2573.Find the String with LCP/Solution.rs new file mode 100644 index 0000000000000..635ec7dbb54e8 --- /dev/null +++ b/solution/2500-2599/2573.Find the String with LCP/Solution.rs @@ -0,0 +1,45 @@ +impl Solution { + pub fn find_the_string(lcp: Vec>) -> String { + let n = lcp.len(); + let mut s = vec!['\0'; n]; + let mut i = 0; + + for c in b'a'..=b'z' { + while i < n && s[i] != '\0' { + i += 1; + } + if i == n { + break; + } + for j in i..n { + if lcp[i][j] > 0 { + s[j] = c as char; + } + } + } + + for i in 0..n { + if s[i] == '\0' { + return "".to_string(); + } + } + + for i in (0..n).rev() { + for j in (0..n).rev() { + if s[i] == s[j] { + if i == n - 1 || j == n - 1 { + if lcp[i][j] != 1 { + return "".to_string(); + } + } else if lcp[i][j] != lcp[i + 1][j + 1] + 1 { + return "".to_string(); + } + } else if lcp[i][j] > 0 { + return "".to_string(); + } + } + } + + s.into_iter().collect() + } +} diff --git a/solution/2500-2599/2574.Left and Right Sum Differences/README.md b/solution/2500-2599/2574.Left and Right Sum Differences/README.md index 6acf1ab41c0cd..a33a29fbb0193 100644 --- a/solution/2500-2599/2574.Left and Right Sum Differences/README.md +++ b/solution/2500-2599/2574.Left and Right Sum Differences/README.md @@ -67,13 +67,13 @@ tags: ### 方法一:前缀和 -我们定义变量 $left$ 表示数组 `nums` 中下标 $i$ 左侧元素之和,变量 $right$ 表示数组 `nums` 中下标 $i$ 右侧元素之和。初始时 $left = 0$, $right = \sum_{i = 0}^{n - 1} nums[i]$。 +我们定义变量 $l$ 表示数组 $\textit{nums}$ 中下标 $i$ 左侧元素之和,变量 $r$ 表示数组 $\textit{nums}$ 中下标 $i$ 右侧元素之和。初始时 $l = 0$, $r = \sum_{i = 0}^{n - 1} \textit{nums}[i]$。 -遍历数组 `nums`,对于当前遍历到的数字 $x$,我们更新 $right = right - x$,此时 $left$ 和 $right$ 分别表示数组 `nums` 中下标 $i$ 左侧元素之和和右侧元素之和。我们将 $left$ 和 $right$ 的差的绝对值加入答案数组 `ans` 中,然后更新 $left = left + x$。 +遍历数组 $\textit{nums}$,对于当前遍历到的数字 $x$,我们更新 $r = r - x$,此时 $l$ 和 $r$ 分别表示数组 $\textit{nums}$ 中下标 $i$ 左侧元素之和和右侧元素之和。我们将 $l$ 和 $r$ 的差的绝对值加入答案数组 $\textit{ans}$ 中,然后更新 $l = l + x$。 -遍历完成后,返回答案数组 `ans` 即可。 +遍历结束,返回答案数组 $\textit{ans}$ 即可。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。 +时间复杂度 $O(n)$,其中 $n$ 是数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$,不考虑返回值的空间。 相似题目: @@ -86,13 +86,13 @@ tags: ```python class Solution: - def leftRigthDifference(self, nums: List[int]) -> List[int]: - left, right = 0, sum(nums) + def leftRightDifference(self, nums: List[int]) -> List[int]: + l, r = 0, sum(nums) ans = [] for x in nums: - right -= x - ans.append(abs(left - right)) - left += x + r -= x + ans.append(abs(l - r)) + l += x return ans ``` @@ -100,14 +100,17 @@ class Solution: ```java class Solution { - public int[] leftRigthDifference(int[] nums) { - int left = 0, right = Arrays.stream(nums).sum(); + public int[] leftRightDifference(int[] nums) { + int l = 0, r = 0; + for (int x : nums) { + r += x; + } int n = nums.length; int[] ans = new int[n]; for (int i = 0; i < n; ++i) { - right -= nums[i]; - ans[i] = Math.abs(left - right); - left += nums[i]; + r -= nums[i]; + ans[i] = Math.abs(l - r); + l += nums[i]; } return ans; } @@ -119,13 +122,17 @@ class Solution { ```cpp class Solution { public: - vector leftRigthDifference(vector& nums) { - int left = 0, right = accumulate(nums.begin(), nums.end(), 0); - vector ans; - for (int& x : nums) { - right -= x; - ans.push_back(abs(left - right)); - left += x; + vector leftRightDifference(vector& nums) { + int l = 0, r = 0; + for (int x : nums) { + r += x; + } + int n = nums.size(); + vector ans(n); + for (int i = 0; i < n; ++i) { + r -= nums[i]; + ans[i] = abs(l - r); + l += nums[i]; } return ans; } @@ -135,17 +142,19 @@ public: #### Go ```go -func leftRigthDifference(nums []int) (ans []int) { - var left, right int +func leftRightDifference(nums []int) []int { + l, r := 0, 0 for _, x := range nums { - right += x + r += x } - for _, x := range nums { - right -= x - ans = append(ans, abs(left-right)) - left += x + n := len(nums) + ans := make([]int, n) + for i, x := range nums { + r -= x + ans[i] = abs(l - r) + l += x } - return + return ans } func abs(x int) int { @@ -159,14 +168,13 @@ func abs(x int) int { #### TypeScript ```ts -function leftRigthDifference(nums: number[]): number[] { - let left = 0, - right = nums.reduce((a, b) => a + b); +function leftRightDifference(nums: number[]): number[] { + let [l, r] = [0, nums.reduce((a, b) => a + b, 0)]; const ans: number[] = []; for (const x of nums) { - right -= x; - ans.push(Math.abs(left - right)); - left += x; + r -= x; + ans.push(Math.abs(l - r)); + l += x; } return ans; } @@ -176,17 +184,16 @@ function leftRigthDifference(nums: number[]): number[] { ```rust impl Solution { - pub fn left_rigth_difference(nums: Vec) -> Vec { - let mut left = 0; - let mut right = nums.iter().sum::(); - nums.iter() - .map(|v| { - right -= v; - let res = (left - right).abs(); - left += v; - res - }) - .collect() + pub fn left_right_difference(nums: Vec) -> Vec { + let mut l = 0; + let mut r: i32 = nums.iter().sum(); + let mut ans = Vec::with_capacity(nums.len()); + for x in nums { + r -= x; + ans.push((l - r).abs()); + l += x; + } + ans } } ``` @@ -197,101 +204,22 @@ impl Solution { /** * Note: The returned array must be malloced, assume caller calls free(). */ -int* leftRigthDifference(int* nums, int numsSize, int* returnSize) { - int left = 0; - int right = 0; - for (int i = 0; i < numsSize; i++) { - right += nums[i]; - } - int* ans = malloc(sizeof(int) * numsSize); - for (int i = 0; i < numsSize; i++) { - right -= nums[i]; - ans[i] = abs(left - right); - left += nums[i]; - } +int* leftRightDifference(int* nums, int numsSize, int* returnSize) { *returnSize = numsSize; - return ans; -} -``` - - - - - - - -### 方法二 - - - -#### TypeScript + int* ans = (int*) malloc(sizeof(int) * numsSize); -```ts -function leftRigthDifference(nums: number[]): number[] { - let left = 0; - let right = nums.reduce((r, v) => r + v); - return nums.map(v => { - right -= v; - const res = Math.abs(left - right); - left += v; - return res; - }); -} -``` - -#### Rust - -```rust -impl Solution { - pub fn left_right_difference(nums: Vec) -> Vec { - let mut ans = vec![]; - - for i in 0..nums.len() { - let mut left = 0; - for j in 0..i { - left += nums[j]; - } - - let mut right = 0; - for k in i + 1..nums.len() { - right += nums[k]; - } - - ans.push((left - right).abs()); - } - - ans + int l = 0, r = 0; + for (int i = 0; i < numsSize; ++i) { + r += nums[i]; } -} -``` - - - - - - -### 方法三 - - - -#### Rust - -```rust -impl Solution { - pub fn left_right_difference(nums: Vec) -> Vec { - let mut left = 0; - let mut right: i32 = nums.iter().sum(); - let mut ans = vec![]; - - for &x in &nums { - right -= x; - ans.push((left - right).abs()); - left += x; - } - - ans + for (int i = 0; i < numsSize; ++i) { + r -= nums[i]; + ans[i] = abs(l - r); + l += nums[i]; } + + return ans; } ``` diff --git a/solution/2500-2599/2574.Left and Right Sum Differences/README_EN.md b/solution/2500-2599/2574.Left and Right Sum Differences/README_EN.md index 187c868de123c..0e6d55b74ca12 100644 --- a/solution/2500-2599/2574.Left and Right Sum Differences/README_EN.md +++ b/solution/2500-2599/2574.Left and Right Sum Differences/README_EN.md @@ -65,13 +65,13 @@ The array answer is [|0 - 0|] = [0]. ### Solution 1: Prefix Sum -We define a variable $left$ to represent the sum of the elements to the left of index $i$ in the array `nums`, and a variable $right$ to represent the sum of the elements to the right of index $i$ in the array `nums`. Initially, $left = 0$, $right = \sum_{i = 0}^{n - 1} nums[i]$. +We define a variable $l$ to represent the sum of elements to the left of index $i$ in the array $\textit{nums}$, and a variable $r$ to represent the sum of elements to the right of index $i$ in the array $\textit{nums}$. Initially, $l = 0$, $r = \sum_{i = 0}^{n - 1} \textit{nums}[i]$. -We iterate over the array `nums`. For the current number $x$ we are iterating over, we update $right = right - x$. At this point, $left$ and $right$ represent the sum of the elements to the left and right of index $i$ in the array `nums`, respectively. We add the absolute difference between $left$ and $right$ to the answer array `ans`, and then update $left = left + x$. +We traverse the array $\textit{nums}$. For the current number $x$, we update $r = r - x$. At this point, $l$ and $r$ represent the sum of elements to the left and right of index $i$ in the array $\textit{nums}$, respectively. We add the absolute difference of $l$ and $r$ to the answer array $\textit{ans}$, then update $l = l + x$. -After the iteration is complete, we return the answer array `ans`. +After the traversal, we return the answer array $\textit{ans}$. -The time complexity is $O(n)$, and the space complexity is $O(1)$. Where $n$ is the length of the array `nums`. +The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$, not counting the space for the return value. Similar problems: @@ -84,13 +84,13 @@ Similar problems: ```python class Solution: - def leftRigthDifference(self, nums: List[int]) -> List[int]: - left, right = 0, sum(nums) + def leftRightDifference(self, nums: List[int]) -> List[int]: + l, r = 0, sum(nums) ans = [] for x in nums: - right -= x - ans.append(abs(left - right)) - left += x + r -= x + ans.append(abs(l - r)) + l += x return ans ``` @@ -98,14 +98,17 @@ class Solution: ```java class Solution { - public int[] leftRigthDifference(int[] nums) { - int left = 0, right = Arrays.stream(nums).sum(); + public int[] leftRightDifference(int[] nums) { + int l = 0, r = 0; + for (int x : nums) { + r += x; + } int n = nums.length; int[] ans = new int[n]; for (int i = 0; i < n; ++i) { - right -= nums[i]; - ans[i] = Math.abs(left - right); - left += nums[i]; + r -= nums[i]; + ans[i] = Math.abs(l - r); + l += nums[i]; } return ans; } @@ -117,13 +120,17 @@ class Solution { ```cpp class Solution { public: - vector leftRigthDifference(vector& nums) { - int left = 0, right = accumulate(nums.begin(), nums.end(), 0); - vector ans; - for (int& x : nums) { - right -= x; - ans.push_back(abs(left - right)); - left += x; + vector leftRightDifference(vector& nums) { + int l = 0, r = 0; + for (int x : nums) { + r += x; + } + int n = nums.size(); + vector ans(n); + for (int i = 0; i < n; ++i) { + r -= nums[i]; + ans[i] = abs(l - r); + l += nums[i]; } return ans; } @@ -133,17 +140,19 @@ public: #### Go ```go -func leftRigthDifference(nums []int) (ans []int) { - var left, right int +func leftRightDifference(nums []int) []int { + l, r := 0, 0 for _, x := range nums { - right += x + r += x } - for _, x := range nums { - right -= x - ans = append(ans, abs(left-right)) - left += x + n := len(nums) + ans := make([]int, n) + for i, x := range nums { + r -= x + ans[i] = abs(l - r) + l += x } - return + return ans } func abs(x int) int { @@ -157,14 +166,13 @@ func abs(x int) int { #### TypeScript ```ts -function leftRigthDifference(nums: number[]): number[] { - let left = 0, - right = nums.reduce((a, b) => a + b); +function leftRightDifference(nums: number[]): number[] { + let [l, r] = [0, nums.reduce((a, b) => a + b, 0)]; const ans: number[] = []; for (const x of nums) { - right -= x; - ans.push(Math.abs(left - right)); - left += x; + r -= x; + ans.push(Math.abs(l - r)); + l += x; } return ans; } @@ -174,17 +182,16 @@ function leftRigthDifference(nums: number[]): number[] { ```rust impl Solution { - pub fn left_rigth_difference(nums: Vec) -> Vec { - let mut left = 0; - let mut right = nums.iter().sum::(); - nums.iter() - .map(|v| { - right -= v; - let res = (left - right).abs(); - left += v; - res - }) - .collect() + pub fn left_right_difference(nums: Vec) -> Vec { + let mut l = 0; + let mut r: i32 = nums.iter().sum(); + let mut ans = Vec::with_capacity(nums.len()); + for x in nums { + r -= x; + ans.push((l - r).abs()); + l += x; + } + ans } } ``` @@ -195,101 +202,22 @@ impl Solution { /** * Note: The returned array must be malloced, assume caller calls free(). */ -int* leftRigthDifference(int* nums, int numsSize, int* returnSize) { - int left = 0; - int right = 0; - for (int i = 0; i < numsSize; i++) { - right += nums[i]; - } - int* ans = malloc(sizeof(int) * numsSize); - for (int i = 0; i < numsSize; i++) { - right -= nums[i]; - ans[i] = abs(left - right); - left += nums[i]; - } +int* leftRightDifference(int* nums, int numsSize, int* returnSize) { *returnSize = numsSize; - return ans; -} -``` - - - - - - - -### Solution 2 - - - -#### TypeScript + int* ans = (int*) malloc(sizeof(int) * numsSize); -```ts -function leftRigthDifference(nums: number[]): number[] { - let left = 0; - let right = nums.reduce((r, v) => r + v); - return nums.map(v => { - right -= v; - const res = Math.abs(left - right); - left += v; - return res; - }); -} -``` - -#### Rust - -```rust -impl Solution { - pub fn left_right_difference(nums: Vec) -> Vec { - let mut ans = vec![]; - - for i in 0..nums.len() { - let mut left = 0; - for j in 0..i { - left += nums[j]; - } - - let mut right = 0; - for k in i + 1..nums.len() { - right += nums[k]; - } - - ans.push((left - right).abs()); - } - - ans + int l = 0, r = 0; + for (int i = 0; i < numsSize; ++i) { + r += nums[i]; } -} -``` - - - - - - -### Solution 3 - - - -#### Rust - -```rust -impl Solution { - pub fn left_right_difference(nums: Vec) -> Vec { - let mut left = 0; - let mut right: i32 = nums.iter().sum(); - let mut ans = vec![]; - - for &x in &nums { - right -= x; - ans.push((left - right).abs()); - left += x; - } - - ans + for (int i = 0; i < numsSize; ++i) { + r -= nums[i]; + ans[i] = abs(l - r); + l += nums[i]; } + + return ans; } ``` diff --git a/solution/2500-2599/2574.Left and Right Sum Differences/Solution.c b/solution/2500-2599/2574.Left and Right Sum Differences/Solution.c index 2df8f4be681ff..16aeb485d5054 100644 --- a/solution/2500-2599/2574.Left and Right Sum Differences/Solution.c +++ b/solution/2500-2599/2574.Left and Right Sum Differences/Solution.c @@ -1,18 +1,20 @@ /** * Note: The returned array must be malloced, assume caller calls free(). */ -int* leftRigthDifference(int* nums, int numsSize, int* returnSize) { - int left = 0; - int right = 0; - for (int i = 0; i < numsSize; i++) { - right += nums[i]; +int* leftRightDifference(int* nums, int numsSize, int* returnSize) { + *returnSize = numsSize; + int* ans = (int*) malloc(sizeof(int) * numsSize); + + int l = 0, r = 0; + for (int i = 0; i < numsSize; ++i) { + r += nums[i]; } - int* ans = malloc(sizeof(int) * numsSize); - for (int i = 0; i < numsSize; i++) { - right -= nums[i]; - ans[i] = abs(left - right); - left += nums[i]; + + for (int i = 0; i < numsSize; ++i) { + r -= nums[i]; + ans[i] = abs(l - r); + l += nums[i]; } - *returnSize = numsSize; + return ans; -} \ No newline at end of file +} diff --git a/solution/2500-2599/2574.Left and Right Sum Differences/Solution.cpp b/solution/2500-2599/2574.Left and Right Sum Differences/Solution.cpp index 6dc1a8e3ce329..6563e126d000e 100644 --- a/solution/2500-2599/2574.Left and Right Sum Differences/Solution.cpp +++ b/solution/2500-2599/2574.Left and Right Sum Differences/Solution.cpp @@ -1,13 +1,17 @@ class Solution { public: - vector leftRigthDifference(vector& nums) { - int left = 0, right = accumulate(nums.begin(), nums.end(), 0); - vector ans; - for (int& x : nums) { - right -= x; - ans.push_back(abs(left - right)); - left += x; + vector leftRightDifference(vector& nums) { + int l = 0, r = 0; + for (int x : nums) { + r += x; + } + int n = nums.size(); + vector ans(n); + for (int i = 0; i < n; ++i) { + r -= nums[i]; + ans[i] = abs(l - r); + l += nums[i]; } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/2500-2599/2574.Left and Right Sum Differences/Solution.go b/solution/2500-2599/2574.Left and Right Sum Differences/Solution.go index 83cbcf7817d3f..5a1a6945c99ea 100644 --- a/solution/2500-2599/2574.Left and Right Sum Differences/Solution.go +++ b/solution/2500-2599/2574.Left and Right Sum Differences/Solution.go @@ -1,14 +1,16 @@ -func leftRigthDifference(nums []int) (ans []int) { - var left, right int +func leftRightDifference(nums []int) []int { + l, r := 0, 0 for _, x := range nums { - right += x + r += x } - for _, x := range nums { - right -= x - ans = append(ans, abs(left-right)) - left += x + n := len(nums) + ans := make([]int, n) + for i, x := range nums { + r -= x + ans[i] = abs(l - r) + l += x } - return + return ans } func abs(x int) int { @@ -16,4 +18,4 @@ func abs(x int) int { return -x } return x -} \ No newline at end of file +} diff --git a/solution/2500-2599/2574.Left and Right Sum Differences/Solution.java b/solution/2500-2599/2574.Left and Right Sum Differences/Solution.java index bced77444f23e..47e966d6b841b 100644 --- a/solution/2500-2599/2574.Left and Right Sum Differences/Solution.java +++ b/solution/2500-2599/2574.Left and Right Sum Differences/Solution.java @@ -1,13 +1,16 @@ class Solution { - public int[] leftRigthDifference(int[] nums) { - int left = 0, right = Arrays.stream(nums).sum(); + public int[] leftRightDifference(int[] nums) { + int l = 0, r = 0; + for (int x : nums) { + r += x; + } int n = nums.length; int[] ans = new int[n]; for (int i = 0; i < n; ++i) { - right -= nums[i]; - ans[i] = Math.abs(left - right); - left += nums[i]; + r -= nums[i]; + ans[i] = Math.abs(l - r); + l += nums[i]; } return ans; } -} \ No newline at end of file +} diff --git a/solution/2500-2599/2574.Left and Right Sum Differences/Solution.py b/solution/2500-2599/2574.Left and Right Sum Differences/Solution.py index 3b2bf69a3b9d5..4812e9529f758 100644 --- a/solution/2500-2599/2574.Left and Right Sum Differences/Solution.py +++ b/solution/2500-2599/2574.Left and Right Sum Differences/Solution.py @@ -1,9 +1,9 @@ class Solution: - def leftRigthDifference(self, nums: List[int]) -> List[int]: - left, right = 0, sum(nums) + def leftRightDifference(self, nums: List[int]) -> List[int]: + l, r = 0, sum(nums) ans = [] for x in nums: - right -= x - ans.append(abs(left - right)) - left += x + r -= x + ans.append(abs(l - r)) + l += x return ans diff --git a/solution/2500-2599/2574.Left and Right Sum Differences/Solution.rs b/solution/2500-2599/2574.Left and Right Sum Differences/Solution.rs index 9f15c00b28ea8..bcc2f367d6be7 100644 --- a/solution/2500-2599/2574.Left and Right Sum Differences/Solution.rs +++ b/solution/2500-2599/2574.Left and Right Sum Differences/Solution.rs @@ -1,14 +1,13 @@ impl Solution { - pub fn left_rigth_difference(nums: Vec) -> Vec { - let mut left = 0; - let mut right = nums.iter().sum::(); - nums.iter() - .map(|v| { - right -= v; - let res = (left - right).abs(); - left += v; - res - }) - .collect() + pub fn left_right_difference(nums: Vec) -> Vec { + let mut l = 0; + let mut r: i32 = nums.iter().sum(); + let mut ans = Vec::with_capacity(nums.len()); + for x in nums { + r -= x; + ans.push((l - r).abs()); + l += x; + } + ans } } diff --git a/solution/2500-2599/2574.Left and Right Sum Differences/Solution.ts b/solution/2500-2599/2574.Left and Right Sum Differences/Solution.ts index 86e4dd9922237..ff3be023eb4d1 100644 --- a/solution/2500-2599/2574.Left and Right Sum Differences/Solution.ts +++ b/solution/2500-2599/2574.Left and Right Sum Differences/Solution.ts @@ -1,11 +1,10 @@ -function leftRigthDifference(nums: number[]): number[] { - let left = 0, - right = nums.reduce((a, b) => a + b); +function leftRightDifference(nums: number[]): number[] { + let [l, r] = [0, nums.reduce((a, b) => a + b, 0)]; const ans: number[] = []; for (const x of nums) { - right -= x; - ans.push(Math.abs(left - right)); - left += x; + r -= x; + ans.push(Math.abs(l - r)); + l += x; } return ans; -} +}; diff --git a/solution/2500-2599/2574.Left and Right Sum Differences/Solution2.rs b/solution/2500-2599/2574.Left and Right Sum Differences/Solution2.rs deleted file mode 100644 index 4d900ff9a04dd..0000000000000 --- a/solution/2500-2599/2574.Left and Right Sum Differences/Solution2.rs +++ /dev/null @@ -1,21 +0,0 @@ -impl Solution { - pub fn left_right_difference(nums: Vec) -> Vec { - let mut ans = vec![]; - - for i in 0..nums.len() { - let mut left = 0; - for j in 0..i { - left += nums[j]; - } - - let mut right = 0; - for k in i + 1..nums.len() { - right += nums[k]; - } - - ans.push((left - right).abs()); - } - - ans - } -} diff --git a/solution/2500-2599/2574.Left and Right Sum Differences/Solution2.ts b/solution/2500-2599/2574.Left and Right Sum Differences/Solution2.ts deleted file mode 100644 index a9d7a8865f49a..0000000000000 --- a/solution/2500-2599/2574.Left and Right Sum Differences/Solution2.ts +++ /dev/null @@ -1,10 +0,0 @@ -function leftRigthDifference(nums: number[]): number[] { - let left = 0; - let right = nums.reduce((r, v) => r + v); - return nums.map(v => { - right -= v; - const res = Math.abs(left - right); - left += v; - return res; - }); -} diff --git a/solution/2500-2599/2574.Left and Right Sum Differences/Solution3.rs b/solution/2500-2599/2574.Left and Right Sum Differences/Solution3.rs deleted file mode 100644 index 3b26f98186c19..0000000000000 --- a/solution/2500-2599/2574.Left and Right Sum Differences/Solution3.rs +++ /dev/null @@ -1,15 +0,0 @@ -impl Solution { - pub fn left_right_difference(nums: Vec) -> Vec { - let mut left = 0; - let mut right: i32 = nums.iter().sum(); - let mut ans = vec![]; - - for &x in &nums { - right -= x; - ans.push((left - right).abs()); - left += x; - } - - ans - } -}