Find Pivot Index之Python實現
阿新 • • 發佈:2018-12-12
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