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 {
public int pivotIndex(int[] nums) {
if (nums.length <= 0)
return -1;
int pre = 0, post = 0;
for (int i = 0; i < nums.length; i++)
post += nums[i];
for (int i = 0; i < nums.length; i++){
post -= nums[i];
if (pre == post)
return i;
pre += nums[i];
}
return -1;
}
}

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

  1. Python解Leetcode: 724. Find Pivot Index

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

  2. 【Leetcode_easy】724. Find Pivot Index

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

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

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

  4. 724. Find Pivot Index

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

  5. [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 ...

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

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

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

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

  8. C#LeetCode刷题之#724-寻找数组的中心索引( Find Pivot Index)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3742 访问. 给定一个整数类型的数组 nums,请编写一个能够返 ...

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

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

随机推荐

  1. [ SSH框架 ] Struts2框架学习之二

    一.Struts2访问Servlet的API 前面已经对 Struts2的流程已经执行完成了,但是如果表单中有参数如何进行接收又或者我们需要向页面保存一些数据,又要如何完成呢?我们可以通过学习 Str ...

  2. SSH密钥认证添加方法和一些实用配置

    更改SSH端口号 用账号密码进入主机 sudo nano /etc/ssh/sshd-config 再其中添加Port 22等或改变该条 添加公钥到主机 cd ~ sudo mkdir .ssh 此处 ...

  3. Nginx负载均衡和反向代理

    1:反向代理 代理就是中介,那有反向代理就有正向代理,两者的区别是什么嘞? 正向代理隐藏真实客户端,服务端不知道实际发起请求的客户端.,proxy和client同属一个LAN,对server透明: 反 ...

  4. va_start,va_arg,va_end的使用

    一.在C中,当我们无法列出传递函数的所有实参的类型和数目时,可以用省略号指定参数表. void fun(...); void fun(parm_list,...); #include <stdi ...

  5. 深入理解.net - 3.类型Type

    说到类型,.NET技术是基于通用类型系统(CTS,Common Type System)的,而CTS又是构建于公共语言架构(CLI,Common Language Infrastructure)之上, ...

  6. Version 1.6.0 of the JVM is not suitable for the this product.Version:1.8 or greater is required

    这个问题时在打开eclipse时报的一个错误,报这个问题的意思我们都明白,说的就是当前版本的jdk版本太低,eclipse需要更高版本的jdk. 那就下一个更高版本的jdk就可以啦,这里我要说说我当时 ...

  7. ubuntu18.04安装安装JDK

    1.前提准备: 下载JDK:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 2. ...

  8. SSM-MyBatis-17:Mybatis中一级缓存(主要是一级缓存存在性的证明,增删改对一级缓存会造成什么影响)

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 缓存------------------------------------------> 很熟悉的一个 ...

  9. 出现“Unable to resolve target 'android-XXX'”怎么处理?

    这句话的中文意思就是说安卓API版本没有能适配22的,需要将sdk更新一下,必要的话,adt都要跟着更新,但就我尝试来看,还是要先更新adt,然后您或许发现,这个表单好不容易刷出来,但是没有我们想要的 ...

  10. python自定义库文件路径

    各有各的小烦恼,各有的小期待 这是人家私事,不要大嘴巴 在Pycharm中import whois时,总是失败 原因是安装了python3.x相关操作过程,将环境变量path中关于Python的配置c ...