博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
724.Find Pivot Index
阅读量:5165 次
发布时间:2019-06-13

本文共 1256 字,大约阅读时间需要 4 分钟。

Given an array of integers nums, write a method that returns the "pivot" index of this array.

We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.

If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.

Example 1:

Input:

nums = [1, 7, 3, 6, 5, 6]
Output: 3
Explanation:
The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.
Also, 3 is the first index where this occurs.

Example 2:

Input:

nums = [1, 2, 3]
Output: -1
Explanation:
There is no index that satisfies the conditions in the problem statement.

Note:

The length of nums will be in the range [0, 10000].
Each element nums[i] will be an integer in the range [-1000, 1000].

class Solution:    def pivotIndex(self, nums):        """        :type nums: List[int]        :rtype: int        """        if len(nums)<=1:            return -1        n = sum(nums)        cur = 0        for i in range(len(nums)):            if cur==(n-nums[i])/2:                return i            cur += nums[i]        return -1

转载于:https://www.cnblogs.com/bernieloveslife/p/9769446.html

你可能感兴趣的文章
android上传文件到服务器
查看>>
我回答了90%的面试题,为什么还被拒?
查看>>
Html - Table 表头固定和 tbody 设置 height 在IE不起作用的解决
查看>>
iOS SVN终端指令
查看>>
Linux如何更新软件源
查看>>
NYOJ-289 苹果 又是一个典型的01背包和上题一样没啥好说的
查看>>
HDU 2262 回溯算法 递归枚举
查看>>
九度0J 1374 所有员工年龄排序
查看>>
listview初始化后仍为空
查看>>
无刷新分页
查看>>
SIFT算法
查看>>
git各种撤销操作
查看>>
每天努力一点之SQL
查看>>
UINavigationBar-使用总结
查看>>
夺命雷公狗jquery---11属性操作
查看>>
linux 常用命令
查看>>
display属性和属性值(18个属性值,常见面试题)
查看>>
微信小程序图片使用示例
查看>>
Ubuntu16.04+cuda8.0rc+opencv3.1.0+caffe+Theano+torch7搭建教程
查看>>
CSS基本相关内容--中秋特别奉献
查看>>