1. 程式人生 > >Find Pivot Index之Python實現

Find Pivot Index之Python實現

def solution01(self, nums):
        startTime = time.time()     # 求當前時間
        index = -1
        for i in range(0, len(nums)):
            sum_left = 0
            sum_right = 0
            # 求i左邊所有值的和
            for j in range(0, i):
                sum_left += nums[j]
            # 求i右邊所有值的和
            for k in range(i + 1, len(nums)):
                sum_right += nums[k]
            # 判斷兩個和是否相等,相等則跳出迴圈
            if sum_left == sum_right:
                index = i
                break
        endTime = time.time()       # 求當前時間
        print("程式執行時間:", (endTime - startTime))      #列印程式執行時間(單位:秒)
        return index