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

Commit 242f75b

Browse files
Update 724._Find_Pivot_Index.md
1 parent 749469d commit 242f75b

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

‎docs/Leetcode_Solutions/724._Find_Pivot_Index.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,40 @@ class Solution(object):
7373
return i
7474
return -1
7575
```
76+
77+
> 思路 2
78+
******- 时间复杂度: O(N)******- 空间复杂度: O(1)******
79+
80+
后面发现其实空间只要O(1)就可以了,就是用left_sum, right_sum分别代表左右两边的和,初始化为0和sum(nums)
81+
82+
```python
83+
class Solution(object):
84+
def pivotIndex(self, nums):
85+
"""
86+
:type nums: List[int]
87+
:rtype: int
88+
"""
89+
left_sum, right_sum = 0, sum(nums)
90+
for idx, num in enumerate(nums):
91+
right_sum -= num
92+
if left_sum == right_sum:
93+
return idx
94+
left_sum += num
95+
return -1
96+
```
97+
98+
99+
100+
101+
102+
103+
104+
105+
106+
107+
108+
109+
110+
111+
112+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /