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. 用bootstrap 分页插件问题

    如果页面加载js 一定要记得引入分页的东西 ,不然不会有数据, <script src="${path}/js/bootstrap-paginator.min.js"> ...

  2. Maven项目管理工具

    Maven项目管理工具 白面郎君 Svn eclipse maven量级 1 Maven的简介 1.1 什么是maven 是apache下的一个开源项目,是纯java开发,并且只是用来管理java项目 ...

  3. Python_正则表达式一

    ''' 常用的正则表达式元字符 . 匹配换行符以外的任意单个字符 * 匹配位于'*'之前的字符或子模的0次或多次出现 + 匹配位于'+'之前的字符或子模式的1次或多次出现 - 用在[]之内用来表示范围 ...

  4. 如何使用 toml 配置 SpaceVim

    配置 SpaceVim 主要包括以下几个内容: 设置 SpaceVim 选项 启动/禁用模块 添加自定义插件 添加自定义按键映射以及插件配置 设置SpaceVim选项 原先,在 init.vim 文件 ...

  5. IntelliJ IDEA(九) :酷炫插件系列

    最近项目比较忙,很久没有更新IDEA系列了,今天介绍一下IDEA的一些炫酷的插件,IDEA强大的插件库,不仅能给我们带来一些开发的便捷,还能提高我们的与众不同. 1.插件的安装 打开setting文件 ...

  6. python3中用HTMLTestRunner.py报ImportError: No module named 'StringIO'解决办法

    .原因是官网的是python2语法写的,看官手动把官网的HTMLTestRunner.py改成python3的语法: 参考:http://bbs.chinaunix.net/thread-415474 ...

  7. vue2.0组件通信小总结

    1.父组件->子组件 父组件 <parent> <child :child-msg="msg"></child>//这里必须要用 - 代替 ...

  8. leetcode 26 80 删除已排序数组中重复的数据

    80. Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if dupli ...

  9. java基础学习周计划之3--每周一练

    每周一练第一周 一. 关键代码:1.斐波那契数列指的是这样一个数列 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...数列第一项和第二项是1, 从第三项开始, ...

  10. netty源码分析之揭开reactor线程的面纱(二)

    如果你对netty的reactor线程不了解,建议先看下上一篇文章netty源码分析之揭开reactor线程的面纱(一),这里再把reactor中的三个步骤的图贴一下 reactor线程 我们已经了解 ...