mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-22 08:21:12 -04:00
Create pivot_index_array.py
This commit is contained in:
parent
0963a43bcc
commit
b467dca561
1 changed files with 19 additions and 0 deletions
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…
Add table
Add a link
Reference in a new issue