其他分享
首页 > 其他分享> > 求中心下标

求中心下标

作者:互联网

def fun1(li: list):
if len(li) == 1: return 1
sum_num = sum(li)
res = 0
for i in range(len(li)):
res += li[i]
if res == sum_num:
return i
sum_num -= li[i]

return -1


def fun2(li: list):
sum_num = sum(li)
res = 0
for i in range(len(li)):
if 2 * res + li[i] == sum_num:
return i
res += li[i]
return -1


li = [1, 7, 3, 6, 5, 6]
# li = [1,1]
print(fun2(li))

标签:return,中心,res,sum,len,li,num,下标
来源: https://www.cnblogs.com/bigcoolcool/p/16268523.html