8000 feat: solved 724. Find Pivot Index · realtemirov/leetcode@dc39aa1 · GitHub
[go: up one dir, main page]

Skip to content

Commit dc39aa1

Browse files
committed
feat: solved 724. Find Pivot Index
1 parent 52925a7 commit dc39aa1

File tree

6 files changed

+92
-1
lines changed

6 files changed

+92
-1
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 724. Find Pivot Index
2+
3+
🟩 Easy
4+
5+
## Solution
6+
7+
My Solution
8+
9+
```go
10+
func pivotIndex(nums []int) int {
11+
sum := 0
12+
for _, num := range nums {
13+
sum += num
14+
}
15+
16+
left := 0
17+
for i, num := range nums {
18+
sum -= num
19+
if left == sum {
20+
return i
21+
}
22+
left += num
23+
}
24+
25+
return -1
26+
}
27+
```
28+
29+
![result](724.png)
30+
31+
Leetcode: [link](https://leetcode.com/problems/find-pivot-index/)

Problems/0724/724.png

37 KB
Loading

Problems/0724/solution.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package problem0724
2+
3+
func PivotIndex(nums []int) int {
4+
sum := 0
5+
for _, num := range nums {
6+
sum += num
7+
}
8+
9+
left := 0
10+
for i, num := range nums {
11+
sum -= num
12+
if left == sum {
13+
return i
14+
}
15+
left += num
16+
}
17+
18+
return -1
19+
}

Problems/0724/solution_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package problem0724_test
2+
3+
import (
4+
"testing"
5+
6+
problem0724 "github.com/realtemirov/leetcode/Problems/0724"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestSolution(t *testing.T) {
11+
testCases := []struct {
12+
name string
13+
cases []int
14+
expected int
15+
}{
16+
{
17+
name: "Test 1",
18+
cases: []int{1, 7, 3, 6, 5, 6},
19+
expected: 3,
20+
},
21+
{
22+
name: "Test 2",
23+
cases: []int{1, 2, 3},
24+
expected: -1,
25+
},
26+
{
27+
name: "Test 3",
28+
cases: []int{2, 1, -1},
29+
expected: 0,
30+
},
31+
}
32+
33+
for _, tc := range testCases {
34+
t.Run(tc.name, func(t *testing.T) {
35+
result := problem0724.PivotIndex(tc.cases)
36+
require.Equal(t, tc.expected, result, "expected: %v, result: %v", tc.expected, result)
37+
})
38+
}
39+
}

Problems/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
* 🟩 Easy - [704. Binary Search](<./0704/704. Binary Search.md>)
5858
* 🟩 Easy - [705. Design HashSet](<./0705/705. Design HashSet.md>)
5959
* 🟩 Easy - [709. To Lower Case](<./0709/709. To Lower Case.md>)
60+
* 🟩 Easy - [724. Find Pivot Index](<./0724/724. Find Pivot Index.md>)
6061
* 🟩 Easy - [876. Middle of the Linked List](<./0876/876. Middle of the Linked List.md>)
6162
* 🟩 Easy - [860. Lemonade Change](<./0860/860. Lemonade Change.md>)
6263
* 🟩 Easy - [896. Monotonic Array](<./0896/896. Monotonic Array.md>)

SUMMARY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@
112112
* [704. Binary Search](<./Problems/0704/704. Binary Search.md>)
113113
* [705. Design HashSet](<./Problems/0705/705. Design HashSet.md>)
114114
* [709. To Lower Case](<./Problems/0709/709. To Lower Case.md>)
115+
* [724. Find Pivot Index](<./Problems/0724/724. Find Pivot Index.md>)
115116
* [876. Middle of the Linked List](<./Problems/0876/876. Middle of the Linked List.md>)
116117
* [896. Monotonic Array](<./Problems/0896/896. Monotonic Array.md>)
117118
* [860. Lemonade Change](<./Problems/0860/860. Lemonade Change.md>)
@@ -207,7 +208,7 @@
207208
* [647. Palindromic Substrings](<Problems/647. Palindromic Substrings.md>)
208209
* [661. Image Smoother](<Problems/661. Image Smoother.md>)
209210
210-
* [724. Find Pivot Index](<Problems/724. Find Pivot Index.md>)
211+
211212
* [746. Min Cost Climbing Stairs](<Problems/746. Min Cost Climbing Stairs.md>)
212213
* [747. Largest Number At Least Twice of Others](<Problems/747. Largest Number At Least Twice of Others.md>)
213214
* [867. Transpose Matrix](<Problems/867. Transpose Matrix.md>)

0 commit comments

Comments
 (0)
0