Skip to content

Commit 52953c5

Browse files
authored
Create find-the-middle-index-in-array.py
1 parent 8ca203d commit 52953c5

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def findMiddleIndex(self, nums):
6+
"""
7+
:type nums: List[int]
8+
:rtype: int
9+
"""
10+
total = sum(nums)
11+
accu = 0
12+
for i, x in enumerate(nums):
13+
if accu*2 == total-x:
14+
return i
15+
accu += x
16+
return -1

0 commit comments

Comments
 (0)