8000 :sparkles: Added solution to 724 · ninefive/awesome-golang-leetcode@72ceaf9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 72ceaf9

Browse files
author
Sathish Babu
committed
✨ Added solution to 724
1 parent 5819cbb commit 72ceaf9

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

src/0724.Find-Pivot-Index/README.md

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,29 @@
55
66
## Description
77

8+
Given an array of integers nums, write a method that returns the "pivot" index of this array.
9+
10+
We define the pivot index as the index where the sum of all the numbers to the left of the index is equal to the sum of all the numbers to the right of the index.
11+
12+
If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.
13+
814
**Example 1:**
915

1016
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
17+
Input: nums = [1,7,3,6,5,6]
18+
Output: 3
19+
Explanation:
20+
The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.
21+
Also, 3 is the first index where this occurs.
22+
```
23+
24+
**Example 2:**
25+
26+
```
27+
Input: nums = [1,2,3]
28+
Output: -1
29+
Explanation:
30+
There is no index that satisfies the conditions in the problem statement.
1331
```
1432

1533
## 题意

src/0724.Find-Pivot-Index/Solution.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) int {
4+
n := len(nums)
5+
if n < 1 {
6+
return -1
7+
}
8+
sum := 0
9+
for _, num := range nums {
10+
sum += num
11+
}
12+
tsum := 0
13+
for i, num := range nums {
14+
sum -= num
15+
if tsum == sum {
16+
return i
17+
}
18+
tsum += num
19+
}
20+
return -1
521
}

src/0724.Find-Pivot-Index/Solution_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ 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+
{"TestCase", []int{1, 7, 3, 6, 5, 6}, 3},
17+
{"TestCase", []int{1, 2, 3}, -1},
18+
{"TestCase", []int{}, -1},
1919
}
2020

2121
// 开始测试

0 commit comments

Comments
 (0)
0