Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit72ceaf9

Browse files
author
Sathish Babu
committed
✨ Added solution to 724
1 parent5819cbb commit72ceaf9

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-
funcSolution(xbool)bool {
4-
returnx
3+
funcSolution(nums []int)int {
4+
n:=len(nums)
5+
ifn<1 {
6+
return-1
7+
}
8+
sum:=0
9+
for_,num:=rangenums {
10+
sum+=num
11+
}
12+
tsum:=0
13+
fori,num:=rangenums {
14+
sum-=num
15+
iftsum==sum {
16+
returni
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
namestring
13-
inputsbool
14-
expectbool
13+
inputs[]int
14+
expectint
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)

[8]ページ先頭

©2009-2025 Movatter.jp