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]

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(vector<int>& nums) {
int sum = accumulate(nums.begin(), nums.end(), );
int ts = ;
for (int i = ; i < nums.size(); i++)
{
if (sum - ts - nums[i] == ts) return i;
else ts += nums[i];
}
return -;
}
};

解析:本题找第i的元素之前和之后的和是否相等,动态规划问题。

Array-Find Pivot Index的更多相关文章

  1. 724. Find Pivot Index

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

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

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

  3. [Leetcode]724. Find Pivot Index

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

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

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

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

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

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

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

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

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

  8. 【Leetcode_easy】724. Find Pivot Index

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

  9. Python解Leetcode: 724. Find Pivot Index

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

随机推荐

  1. 七大排序的个人总结(二) 归并排序(Merge

    七大排序的个人总结(二)   归并排序(Merge  归并排序(Merge Sort): 归并排序是一个相当“稳定”的算法对于其它排序算法,比如希尔排序,快速排序和堆排序而言,这些算法有所谓的最好与最 ...

  2. 并发编程(四)TaskFuture

    并发编程(四)TaskFuture ExecutorService executorService = Executors.newSingleThreadExecutor(); Future<O ...

  3. com.opensymphony.xwork2.config.ConfigurationManager.addConfigurationProvider

    一月 31, 2016 5:06:31 下午 org.apache.catalina.core.StandardContext filterStart 严重: Exception starting f ...

  4. python常用模块及面向对象(一)

    目录: 常用模块之time模块 常用模块之random模块 常用模块之os模块 常用模块之sys模块 常用模块之subprocess模块 常用模块之json模块 常用模块之pickle模块 常用模块之 ...

  5. c++中类的静态成员对象

    在c++中,可以声明一个静态的成员对象,但是此时仅仅声明,没有定义,也不会创建这个内部的静态成员对象.只有在类体外部定以后才能创建这个对象. #include<iostream> usin ...

  6. 非常实用的windows运行打开服务命令

    1.注册表-->regedit.exe 2.本地服务设置-->services.msc 3.远程桌面连接-->mstsc 4.检查windows版本-->winver 5.组策 ...

  7. 2018.06.30 BZOJ3083: 遥远的国度(换根树剖)

    3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 512 MB Description 描述 zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国 ...

  8. 2018.07.08 NOIP模拟 好数(线段树)

    好数 题目背景 SOURCE:NOIP2016-AHSDFZ T3 题目描述 我们定义一个非负整数是"好数",当且仅当它符合以下条件之一: 1. 这个数是 0 或 1 . 2. 所 ...

  9. LA 3708 && POJ 3154 Graveyard (思维)

    题意:在周长为10000的圆上等距分布着n个雕塑,现在又加入m个,现在让m+n个等距分布,那就得移动一些原有的雕塑,问你移动的最少总距离是多少. 析:首先我们可以知道,至少有一个雕塑是可以不用移动的, ...

  10. HDU2577 How to Type 2016-09-11 14:05 29人阅读 评论(0) 收藏

    How to Type Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...