Skip to content

Commit bc7c289

Browse files
committed
Merge pull request #52 from Janetyu/janetyu
finish 0724 solution
2 parents d0543da + 9df3022 commit bc7c289

File tree

6 files changed

+239
-2
lines changed

6 files changed

+239
-2
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
var sum, leftSum int
11+
for _, num := range nums {
12+
sum += num
13+
}
14+
for index, num := range nums {
15+
if leftSum*2+num == sum {
16+
return index
17+
}
18+
leftSum += num
19+
}
20+
return -1
21+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question724 struct {
9+
para724
10+
ans724
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para724 struct {
16+
nums []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans724 struct {
22+
n int
23+
}
24+
25+
func Test_Problem724(t *testing.T) {
26+
27+
qs := []question724{
28+
29+
question724{
30+
para724{[]int{1, 7, 3, 6, 5, 6}},
31+
ans724{3},
32+
},
33+
34+
question724{
35+
para724{[]int{1, 2, 3}},
36+
ans724{-1},
37+
},
38+
39+
question724{
40+
para724{[]int{-1, -1, -1, -1, -1, -1}},
41+
ans724{-1},
42+
},
43+
44+
question724{
45+
para724{[]int{-1, -1, -1, -1, -1, 0}},
46+
ans724{2},
47+
},
48+
49+
question724{
50+
para724{[]int{1}},
51+
ans724{0},
52+
},
53+
54+
question724{
55+
para724{[]int{}},
56+
ans724{-1},
57+
},
58+
}
59+
60+
fmt.Printf("------------------------Leetcode Problem 724------------------------\n")
61+
62+
for _, q := range qs {
63+
_, p := q.ans724, q.para724
64+
fmt.Printf("【input】:%v 【output】:%v\n", p, pivotIndex(p.nums))
65+
}
66+
fmt.Printf("\n\n\n")
67+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
给定一个整数类型的数组 nums,请编写一个能够返回数组 “中心索引” 的方法。我们是这样定义数组 中心索引 的:数组中心索引的左侧所有元素相加的和等于右侧所有元素相加的和。如果数组不存在中心索引,那么我们应该返回 -1。如果数组有多个中心索引,那么我们应该返回最靠近左边的那一个。
39+
40+
41+
42+
## 解题思路
43+
44+
- 在数组中,找到一个数,使得它左边的数之和等于它的右边的数之和,如果存在,则返回这个数的下标索引,否作返回 -1。
45+
- 这里面存在一个等式,只需要满足这个等式即可满足条件:leftSum + num[i] = sum - leftSum => 2 * leftSum + num[i] = sum
46+
- 题目提到如果存在多个索引,则返回最左边那个,因此从左开始求和,而不是从右边
47+
48+
## 代码
49+
50+
```go
51+
52+
package leetcode
53+
54+
// 2 * leftSum + num[i] = sum
55+
// 时间: O(n)
56+
// 空间: O(1)
57+
func pivotIndex(nums []int) int {
58+
if len(nums) <= 0 {
59+
return -1
60+
}
61+
var sum, leftSum int
62+
for _, num := range nums {
63+
sum += num
64+
}
65+
for index, num := range nums {
66+
if leftSum*2+num == sum {
67+
return index
68+
}
69+
leftSum += num
70+
}
71+
return -1
72+
}
73+
74+
```

website/content/ChapterFour/0118.Pascals-Triangle.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ Given a non-negative integer numRows, generate the first numRows of Pascal's tri
77

88
![](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif)
99

10-
**Note:** In Pascal's triangle, each number is the sum of the two numbers directly above it.
10+
**Note**: In Pascal's triangle, each number is the sum of the two numbers directly above it.
1111

12-
**Example:**
12+
**Example**:
1313

1414
```
1515
Input: 5
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
给定一个整数类型的数组 nums,请编写一个能够返回数组 “中心索引” 的方法。我们是这样定义数组 中心索引 的:数组中心索引的左侧所有元素相加的和等于右侧所有元素相加的和。如果数组不存在中心索引,那么我们应该返回 -1。如果数组有多个中心索引,那么我们应该返回最靠近左边的那一个。
39+
40+
41+
42+
## 解题思路
43+
44+
- 在数组中,找到一个数,使得它左边的数之和等于它的右边的数之和,如果存在,则返回这个数的下标索引,否作返回 -1。
45+
- 这里面存在一个等式,只需要满足这个等式即可满足条件:leftSum + num[i] = sum - leftSum => 2 * leftSum + num[i] = sum。
46+
- 题目提到如果存在多个索引,则返回最左边那个,因此从左开始求和,而不是从右边。
47+
48+
## 代码
49+
50+
```go
51+
52+
package leetcode
53+
54+
// 2 * leftSum + num[i] = sum
55+
// 时间: O(n)
56+
// 空间: O(1)
57+
func pivotIndex(nums []int) int {
58+
if len(nums) <= 0 {
59+
return -1
60+
}
61+
var sum, leftSum int
62+
for _, num := range nums {
63+
sum += num
64+
}
65+
for index, num := range nums {
66+
if leftSum*2+num == sum {
67+
return index
68+
}
69+
leftSum += num
70+
}
71+
return -1
72+
}
73+
74+
```

website/content/menu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@ headless: true
355355
- [0719.Find-K-th-Smallest-Pair-Distance]({{< relref "/ChapterFour/0719.Find-K-th-Smallest-Pair-Distance.md" >}})
356356
- [0720.Longest-Word-in-Dictionary]({{< relref "/ChapterFour/0720.Longest-Word-in-Dictionary.md" >}})
357357
- [0721.Accounts-Merge]({{< relref "/ChapterFour/0721.Accounts-Merge.md" >}})
358+
- [0724.Find-Pivot-Index]({{< relref "/ChapterFour/0724.Find-Pivot-Index.md" >}})
358359
- [0725.Split-Linked-List-in-Parts]({{< relref "/ChapterFour/0725.Split-Linked-List-in-Parts.md" >}})
359360
- [0726.Number-of-Atoms]({{< relref "/ChapterFour/0726.Number-of-Atoms.md" >}})
360361
- [0729.My-Calendar-I]({{< relref "/ChapterFour/0729.My-Calendar-I.md" >}})

0 commit comments

Comments
 (0)