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].

思路就是l, r 分别记录leftSum, rightSum, 然后每次scan的时候, leftSum + nums[i-1], rightSum - nums[i], 然后比较, 如果相等, 就返回i.

Code

class Solution:
def pivotIndex(self, nums):
nums = [0] + nums
l, r = 0, sum(nums)
for i in range(1, len(nums)):
l += nums[i-1]
r -= nums[i]
if l == r:
return i
return -1

[LeetCode] 724. Find Pivot Index_Easy tag: Dynamic Programming的更多相关文章

  1. [LeetCode] 64. Minimum Path Sum_Medium tag: Dynamic Programming

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

  2. [LeetCode] 139. Word Break_ Medium tag: Dynamic Programming

    Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine ...

  3. [LeetCode] 152. Maximum Product Subarray_Medium tag: Dynamic Programming

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  4. [LeetCode] 55. Jump Game_ Medium tag: Dynamic Programming

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  5. [LeetCode] 97. Interleaving String_ Hard tag: Dynamic Programming

    Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. Example 1: Input: s1 = ...

  6. [LeetCode] 115. Distinct Subsequences_ Hard tag: Dynamic Programming

    Given a string S and a string T, count the number of distinct subsequences of S which equals T. A su ...

  7. [LeetCode] 70. Climbing Stairs_ Easy tag: Dynamic Programming

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  8. [LeetCode] 62. Unique Paths_ Medium tag: Dynamic Programming

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  9. [LeetCode] 198. House Robber _Easy tag: Dynamic Programming

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

随机推荐

  1. Git学习之Git 暂存区

    ============================= 修改文件后是否可以直接提交 ============================ (1) 向文件中追加一行内容  $ echo &quo ...

  2. sea.js常用接口

    seajs.config 用来对 Sea.js 进行配置. seajs.config({ // 指定需要使用的插件 plugins: ['text', 'shim'], // 设置别名,方便调用 al ...

  3. Word 2010 制作文档结构之页码从正文开始设置

    一般技术性文档结构划分: 第一页(首页) 第二页(修改记录页/版本记录页) 第三页(目录) 第四页(正文) 需求: 页脚编码 从正文(即第四页)开始,而不是从首页开始,那么该如何实现? 前提准备: 输 ...

  4. 题目1208:10进制 VS 2进制(进制转换以及大数保存问题)

    题目链接:http://ac.jobdu.com/problem.php?pid=1208 详细链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  5. sort与asort与ksort区别

    sort只依据值从小到大排序,键值不参与排序 asort依据值排序,键值参与排序 ksort依据键值排序,值参与排序 sort只依据值从小到大排序,键值不参与排序. 例 <?php $arr=a ...

  6. 问题记录,如何解决confluence的office预览的时候的乱码问题

    在新的服务器(ubuntu16.04)上安装confluence,预览office的附件的时候,发现中文无法正确显示 在网上搜了一下,搜到一篇官方的文档,是关于这个问题的 问题原因: 在服务器上没有安 ...

  7. VMware激活码,Acrobat激活码

    VMware Workstation Pro key/注册码: VY1DU-2VXDH-08DVQ-PXZQZ-P2KV8 VF58R-28D9P-0882Z-5GX7G-NPUTF YG7XR-4G ...

  8. centos6.9环境下JDK安装

    1.准备jdk安装文件: 这里我使用的是 jdk-7u79-linux-x64.tar.gz 2.在 /usr/local 目录下创建 sotfware目录,并上传JDK文件: 解压文件并修改文件夹为 ...

  9. postgresql----文本搜索类型和检索函数

    postgresql提供两种数据类型用于支持全文检索:tsvector类型产生一个文档(以优化全文检索形式)和tsquery类型用于查询检索. tsvector的值是一个无重复的lexemes排序列表 ...

  10. 1.1Tensorflow训练线性回归模型入门程序

    tensorflow #-*- coding: utf-8 -*- # @Time : 2017/12/19 14:36 # @Author : Z # @Email : S # @File : 1. ...