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. 详细的<select>下拉列表详解

    我们使用表单下拉列表选择数据,如省.市.县.年.月等数据,我们即可使用下拉菜单表单进行设置.select 我下拉列表菜单标签Option为下拉列表数据标签Value 为Option的数据值(用于数据的 ...

  2. php递归实现无限级分类树

      作者: PHP中文网|标签:PHP 递归 无限级树|2017-5-18 18:09   无限级树状图可以说是无限级栏目的一个显著特征,我们接下来就来看看两种不同的写法. 一.数据库设计 1 2 3 ...

  3. jqery对于select级联操作

    问题:今天在做一个需求的时候,有一个级联操作也就是选中下拉框的一列就显示对对应的数据 处理:我在做级联的时候在option的列里面绑定click的事件发现这个事件行不通:查资料发现select触发的是 ...

  4. 智能指针之 shared_ptr

     std::shared_ptr 是通过指针保持对象共享所有权的智能指针.多个 shared_ptr 对象可占有同一对象大概实现了一下,主要实现原理为,共享指针内部持有堆资源 的指针以及引用计数的指针 ...

  5. 软件及博客的markdown支持度的评测

    软件 vscode vscode原生支持markdown,但对数学公式的支持不太好,用 $$包含的数学公式不支持换行,而且在数学公式里面不能输入中文 Typora 非常简洁优美的软件,只有预览页,没有 ...

  6. Robot Framework之测试用例分层实战

    1.1  测试用例的第一层(交互层) 1. 创建项目资源(Resource). 操作步骤: 点”项目名称”->右键,选New Resource,在弹窗Name 输入框输入资源名称 mykeywo ...

  7. .NET之JSON序列化运用

    1.项目引用NuGet包:搜索:Newtonsoft.Json 2.序列号实例 using System; using System.Collections.Generic; using System ...

  8. 导出excel记录

    前言: 记录这篇使用记录,是为了方便以后学习查阅和让没有使用过的人了解一下,其中不足还请见谅.不是很全的文章,大神请绕行. 在项目中我们或多或少的会遇到数据导出到excel表格以便线下查看或者记录一些 ...

  9. 使用FluentScheduler和IIS预加载在asp.net中实现定时任务管理

    FluentScheduler介绍 github地址:https://github.com/fluentscheduler/FluentScheduler FluentScheduler是一个简单的任 ...

  10. 语音识别中的CTC算法的基本原理解释

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文作者:罗冬日 目前主流的语音识别都大致分为特征提取,声学模型,语音模型几个部分.目前结合神经网络的端到端的声学模型训练方法主要CTC和基 ...