Skip to content

Commit dfa1db6

Browse files
authored
Merge pull request 6boris#1283 from 0xff-dev/2574
Add solution and test-cases for problem 2574
2 parents e5b7b76 + 7694703 commit dfa1db6

File tree

3 files changed

+59
-9
lines changed

3 files changed

+59
-9
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# [2574.Left and Right Sum Differences][title]
2+
3+
## Description
4+
You are given a **0-indexed** integer array `nums` of size `n`.
5+
6+
Define two arrays `leftSum` and `rightSum` where:
7+
8+
- `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`.
9+
- `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`.
10+
11+
Return an integer array `answer` of size n where `answer[i] = |leftSum[i] - rightSum[i]|`.
12+
13+
**Example 1:**
14+
15+
```
16+
Input: nums = [10,4,8,3]
17+
Output: [15,1,11,22]
18+
Explanation: The array leftSum is [0,10,14,22] and the array rightSum is [15,11,3,0].
19+
The array answer is [|0 - 15|,|10 - 11|,|14 - 3|,|22 - 0|] = [15,1,11,22].
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input: nums = [1]
26+
Output: [0]
27+
Explanation: The array leftSum is [0] and the array rightSum is [0].
28+
The array answer is [|0 - 0|] = [0].
29+
```
30+
31+
## 结语
32+
33+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
34+
35+
[title]: https://leetcode.com/problems/left-and-right-sum-differences
36+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) []int {
4+
n := len(nums)
5+
left, right := make([]int, n), make([]int, n)
6+
left[0], right[n-1] = 0, 0
7+
for i := 0; i < n-1; i++ {
8+
left[i+1] = nums[i] + left[i]
9+
}
10+
for i := n - 1; i > 0; i-- {
11+
right[i-1] = right[i] + nums[i]
12+
}
13+
for i := 0; i < n; i++ {
14+
left[i] -= right[i]
15+
if left[i] < 0 {
16+
left[i] = -left[i]
17+
}
18+
}
19+
return left
520
}

leetcode/2501-2600/2574.Left-and-Right-Sum-Differences/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{10, 4, 8, 3}, []int{15, 1, 11, 22}},
17+
{"TestCase2", []int{1}, []int{0}},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)