mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-04-29 20:26:07 -04:00
Create pivot_index_array.py
This commit is contained in:
parent
0963a43bcc
commit
b467dca561
19
arrays_and_strings/pivot_index_array.py
Normal file
19
arrays_and_strings/pivot_index_array.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# author: bt3gl
|
||||||
|
|
||||||
|
|
||||||
|
def pivotIndex(nums):
|
||||||
|
|
||||||
|
s = sum(nums)
|
||||||
|
|
||||||
|
left_sum = 0
|
||||||
|
|
||||||
|
for i, x in enumerate(nums):
|
||||||
|
|
||||||
|
if left_sum == (s - left_sum - x):
|
||||||
|
return i
|
||||||
|
|
||||||
|
left_sum += x
|
||||||
|
|
||||||
|
return -1
|
Loading…
x
Reference in New Issue
Block a user