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.

Examlple 2:

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

给定一个数组,找出一个数,左边元素之和等于右边元素之和,存在范围其index,反之-1

假定存在这个数,满足:

  • 1.left + nums[i] + right = sum
  • 2.left = right
  • 3.sum - 2 * left = nums[i]
    public int pivotIndex(int[] nums) {
int sum = 0;
for(int num:nums)
sum += num;
int left=0;
for(int i = 0; i < nums.length; i++)
{ if (i != 0)
left += nums[i-1]; //确保是在‘pivot’的左侧
if(sum - 2 * left == nums[i]) return i;
}
return -1;
}

724. Find Pivot Index的更多相关文章

  1. 【Leetcode_easy】724. Find Pivot Index

    problem 724. Find Pivot Index 题意:先求出数组的总和,然后维护一个当前数组之和curSum,然后对于遍历到的位置,用总和减去当前数字,看得到的结果是否是curSum的两倍 ...

  2. Python解Leetcode: 724. Find Pivot Index

    leetcode 724. Find Pivot Index 题目描述:在数组中找到一个值,使得该值两边所有值的和相等.如果值存在,返回该值的索引,否则返回-1 思路:遍历两遍数组,第一遍求出数组的和 ...

  3. [Leetcode]724. Find Pivot Index

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

  4. 724. Find Pivot Index 找到中轴下标

    [抄题]: Given an array of integers nums, write a method that returns the "pivot" index of th ...

  5. 【LeetCode】724. Find Pivot Index 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和,再遍历 日期 题目地址:https://le ...

  6. [LeetCode] Find Pivot Index 寻找中枢点

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

  7. [Swift]LeetCode724. 寻找数组的中心索引 | Find Pivot Index

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

  8. [LeetCode] 724. Find Pivot Index_Easy tag: Dynamic Programming

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

  9. Array-Find Pivot Index

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

随机推荐

  1. 2715:谁拿了最多奖学金-poj

    总时间限制:  1000ms 内存限制:  65536kB 描述 某校的惯例是在每学期的期末考试之后发放奖学金.发放的奖学金共有五种,获取的条件各自不同: 1) 院士奖学金,每人8000元,期末平均成 ...

  2. 单节点下使用docker部署consul

    部署consul 目前Consul使用的版本是: v1.0.1 本教程适用于刚刚开始学习consul并简单使用consul的同学,可以在短时间内了解conusl,配合官方文档https://www.c ...

  3. buttongroup中content一次性加载的解决方法

    buttongroup一次性加载所有内容的解决方法 如下图所示: 第一步: 设置windowcontainer的autoLoad属性为false(默认情况下autoLoad属性为true,所以会加载所 ...

  4. 关于Unity里动态加载图片

    Resources.Load 使用该方法可以动态加载资源 过程: 1.首先需要在Project面板里创建一个名为Resources的文件夹(名字必须是这个 不能写错啊) 2.把要加载的游戏对象放到该目 ...

  5. javascript语言基础

    js的基本语法 /* 多行注释 * */ //单行注释 // 变量赋值 默认以换行符作为结束符,有分号以分号作为结束符号 var i; i=10; s="hello"; var b ...

  6. 【转】vscode: Visual Studio Code 常用快捷键

    原文链接:https://www.cnblogs.com/bindong/p/6045957.html vscode: Visual Studio Code 常用快捷键 主命令框 F1 或 Ctrl+ ...

  7. heartbeat+DRBD 高可用 双机热备

    heartbeat+DRBD 高可用 双机热备 原创博文http://www.cnblogs.com/elvi/p/7658109.html ## heartbeat+DRBD 高可用 双机热备 # ...

  8. python :ascii codec can't decode byte 0xe8 in posit

    python代码: slide.setAttribute("SlideName", module.slide_name)  slide.setAttribute("Sli ...

  9. PHP去除unicode续:json_encode之后,仅仅有文字,数字不见了的解决方法

    接前文.http://blog.csdn.net/yanzi1225627/article/details/44985487 这么处理了一段时间.确实没发现问题.但近期发现了一个bug.比方输入&qu ...

  10. 解决EJB本地调用“java.lang.ClassCastException: $Proxy96 cannot be cast to com.tgb.ejb.UserManager”异常

    EJB本地调用方式:把Webclient和EJB服务端部署到同一个JBoss,client和server通过一个JVM进行通信. Web客户端本地调用时.需引用EJB服务端打包的jar,不需引用JBo ...