Skip to content

Commit aa5ef0b

Browse files
committed
finish 724 solution
1 parent 7989003 commit aa5ef0b

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package leetcode
2+
3+
// 2 * leftSum + num[i] = sum
4+
// 时间: O(n)
5+
// 空间: O(1)
6+
func pivotIndex(nums []int) int {
7+
if len(nums) <= 0 {
8+
return -1
9+
}
10+
11+
var sum, leftSum int
12+
for _, num := range nums {
13+
sum += num
14+
}
15+
16+
for index, num := range nums {
17+
if leftSum*2+num == sum {
18+
return index
19+
}
20+
leftSum += num
21+
}
22+
return -1
23+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package leetcode
2+
3+
import "testing"
4+
5+
func Test_pivotIndex(t *testing.T) {
6+
type args struct {
7+
nums []int
8+
}
9+
tests := []struct {
10+
name string
11+
args args
12+
want int
13+
}{
14+
{
15+
name: "test_case1",
16+
args: args{[]int{1, 7, 3, 6, 5, 6}},
17+
want: 3,
18+
},
19+
{
20+
name: "test_case2",
21+
args: args{[]int{1,2,3}},
22+
want: -1,
23+
},
24+
{
25+
name: "test_case3",
26+
args: args{[]int{-1,-1,-1,-1,-1,-1}},
27+
want: -1,
28+
},
29+
{
30+
name: "test_case4",
31+
args: args{[]int{-1,-1,-1,-1,-1,0}},
32+
want: 2,
33+
},
34+
{
35+
name: "test_case5",
36+
args: args{[]int{1}},
37+
want: 0,
38+
},
39+
{
40+
name: "test_case5",
41+
args: args{[]int{}},
42+
want: -1,
43+
},
44+
}
45+
for _, tt := range tests {
46+
t.Run(tt.name, func(t *testing.T) {
47+
if got := pivotIndex(tt.args.nums); got != tt.want {
48+
t.Errorf("pivotIndex() = %v, want %v", got, tt.want)
49+
}
50+
})
51+
}
52+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# [724. Find Pivot Index](https://leetcode.com/problems/find-pivot-index/)
2+
3+
4+
## 题目
5+
6+
Given an array of integers nums, write a method that returns the "pivot" index of this array.
7+
8+
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.
9+
10+
If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.
11+
12+
13+
14+
**Example 1:**
15+
16+
Input: nums = [1,7,3,6,5,6]
17+
Output: 3
18+
Explanation:
19+
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.
20+
Also, 3 is the first index where this occurs.
21+
22+
**Example 2:**
23+
24+
Input: nums = [1,2,3]
25+
Output: -1
26+
Explanation:
27+
There is no index that satisfies the conditions in the problem statement.
28+
29+
**Constraints:**
30+
31+
- The length of nums will be in the range [0, 10000].
32+
- Each element nums[i] will be an integer in the range [-1000, 1000].
33+
34+
35+
36+
## 题目大意
37+
38+
39+
在数组中,找到一个数,使得它左边的数之和等于它的右边的数之和,如果存在,则返回这个数的下标索引,否作返回 -1
40+
41+
42+
## 解题思路
43+
44+
- 这里面存在一个等式,只需要满足这个等式即可满足条件:leftSum + num[i] = sum - leftSum => 2 * leftSum + num[i] = sum
45+
- 题目提到如果存在多个索引,则返回最左边那个,因此从左开始求和,而不是从右边
46+
47+

0 commit comments

Comments
 (0)