From 769470321c95250b55b38ac04ceb6651a3dc57e2 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Sun, 10 Aug 2025 16:53:55 +0800 Subject: [PATCH] Add solution and test-cases for problem 2574 --- .../README.md | 36 +++++++++++++++++++ .../Solution.go | 19 ++++++++-- .../Solution_test.go | 13 ++++--- 3 files changed, 59 insertions(+), 9 deletions(-) create mode 100644 leetcode/2501-2600/2574.Left-and-Right-Sum-Differences/README.md diff --git a/leetcode/2501-2600/2574.Left-and-Right-Sum-Differences/README.md b/leetcode/2501-2600/2574.Left-and-Right-Sum-Differences/README.md new file mode 100644 index 000000000..31634255a --- /dev/null +++ b/leetcode/2501-2600/2574.Left-and-Right-Sum-Differences/README.md @@ -0,0 +1,36 @@ +# [2574.Left and Right Sum Differences][title] + +## Description +You are given a **0-indexed** integer array `nums` of size `n`. + +Define two arrays `leftSum` and `rightSum` where: + +- `leftSum[i]` is the sum of elements to the left of the index `i` in the array `nums`. If there is no such element, `leftSum[i] = 0`. +- `rightSum[i]` is the sum of elements to the right of the index `i` in the array `nums`. If there is no such element, `rightSum[i] = 0`. + +Return an integer array `answer` of size n where `answer[i] = |leftSum[i] - rightSum[i]|`. + +**Example 1:** + +``` +Input: nums = [10,4,8,3] +Output: [15,1,11,22] +Explanation: The array leftSum is [0,10,14,22] and the array rightSum is [15,11,3,0]. +The array answer is [|0 - 15|,|10 - 11|,|14 - 3|,|22 - 0|] = [15,1,11,22]. +``` + +**Example 2:** + +``` +Input: nums = [1] +Output: [0] +Explanation: The array leftSum is [0] and the array rightSum is [0]. +The array answer is [|0 - 0|] = [0]. +``` + +## 结语 + +如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] + +[title]: https://leetcode.com/problems/left-and-right-sum-differences +[me]: https://github.com/kylesliu/awesome-golang-algorithm diff --git a/leetcode/2501-2600/2574.Left-and-Right-Sum-Differences/Solution.go b/leetcode/2501-2600/2574.Left-and-Right-Sum-Differences/Solution.go index d115ccf5e..430c816ce 100755 --- a/leetcode/2501-2600/2574.Left-and-Right-Sum-Differences/Solution.go +++ b/leetcode/2501-2600/2574.Left-and-Right-Sum-Differences/Solution.go @@ -1,5 +1,20 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(nums []int) []int { + n := len(nums) + left, right := make([]int, n), make([]int, n) + left[0], right[n-1] = 0, 0 + for i := 0; i < n-1; i++ { + left[i+1] = nums[i] + left[i] + } + for i := n - 1; i > 0; i-- { + right[i-1] = right[i] + nums[i] + } + for i := 0; i < n; i++ { + left[i] -= right[i] + if left[i] < 0 { + left[i] = -left[i] + } + } + return left } diff --git a/leetcode/2501-2600/2574.Left-and-Right-Sum-Differences/Solution_test.go b/leetcode/2501-2600/2574.Left-and-Right-Sum-Differences/Solution_test.go index 14ff50eb4..5712c000f 100755 --- a/leetcode/2501-2600/2574.Left-and-Right-Sum-Differences/Solution_test.go +++ b/leetcode/2501-2600/2574.Left-and-Right-Sum-Differences/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs []int + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{10, 4, 8, 3}, []int{15, 1, 11, 22}}, + {"TestCase2", []int{1}, []int{0}}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }