problem

724. Find Pivot Index

题意:先求出数组的总和,然后维护一个当前数组之和curSum,然后对于遍历到的位置,用总和减去当前数字,看得到的结果是否是curSum的两倍,是的话,那么当前位置就是中枢点,返回即可;否则就将当前数字加到curSum中继续遍历,遍历结束后还没返回,说明没有中枢点,返回-1即可。

solution:

注意,不管有几种方式,最后的结果都是从最左边开始的那个结果,那么我们就直接从最左边开始计算,返回第一个满足条件的即可。

class Solution {
public:
int pivotIndex(vector<int>& nums) {
int sum = accumulate(nums.begin(), nums.end(), );//err..
int curSum = ;
for(int i=; i<nums.size(); ++i)
{
if(sum-nums[i] == *curSum) return i;
curSum += nums[i];
}
return -;
}
};

参考

1. Leetcode_easy_724. Find Pivot Index;

2. Grandyang;

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

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

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

  2. 1229【MySQL】性能优化之 Index Condition Pushdown

    转自http://blog.itpub.net/22664653/viewspace-1210844/  [MySQL]性能优化之 Index Condition Pushdown2014-07-06 ...

  3. Python解Leetcode: 724. Find Pivot Index

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

  4. 【C#】【MySQL】【GridView】删除出现Parameter index is out of range

    [编程语言]C# [数据库]MySQL [控件]GridView [问题描述]GridView控件中自带[删除],[编辑],[选择],三个按钮[编辑],[选择]正常使用,但是在使用删除时,却报错Par ...

  5. 724. Find Pivot Index

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

  6. [Leetcode]724. Find Pivot Index

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

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

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

  8. 【Leetcode_easy】852. Peak Index in a Mountain Array

    problem 852. Peak Index in a Mountain Array solution1: class Solution { public: int peakIndexInMount ...

  9. 【Leetcode_easy】599. Minimum Index Sum of Two Lists

    problem 599. Minimum Index Sum of Two Lists 题意:给出两个字符串数组,找到坐标位置之和最小的相同的字符串. 计算两个的坐标之和,如果与最小坐标和sum相同, ...

随机推荐

  1. MySQL之JDBC插入、修改、删除封装集于一个方法

    1. 先建四个包 ① com.tz.entity 实体包 ② com.tz.dao 数据访问层包 -> com.tz.dao.impl 实现包 ③ com.tz.util 工具类包 ④ com. ...

  2. vue 数组更新检测注意事项

  3. WebForm FindControl的使用方法

    Control.FindControl (String):在当前的命名容器中搜索带指定 id参数的服务器控件. 有点类似javascript中的getElementById(string); 简单的例 ...

  4. [51Nod 1238] 最小公倍数之和 (恶心杜教筛)

    题目描述 求∑i=1N∑j=1Nlcm(i,j)\sum_{i=1}^N\sum_{j=1}^Nlcm(i,j)i=1∑N​j=1∑N​lcm(i,j) 2<=N<=10102<=N ...

  5. Maximum Average Subarray II

    Description Given an array with positive and negative numbers, find the maximum average subarray whi ...

  6. Nginx 负载均衡条件下 Tomcat 共享Session (Java)(一)

    1.修改tomcat 下 conf/context.xml  在</Context>里面加入以下代码 <Valve className="com.orangefunctio ...

  7. HTML 文字前面怎么加空格

    HTML  写文字开头需要用空格时  就需要在文字前面     使用时两个 为一个字的距离 使用后

  8. mybatisplus构造器 condition

    不为null和不为“”的才会加入到sql语句中

  9. Codeforces Round #589 (Div. 2)

    目录 Contest Info Solutions A. Distinct Digits B. Filling the Grid C. Primes and Multiplication D. Com ...

  10. CPU占用高系统反应慢之问题定位

    今天在看到公司群里有关于测试反应测试服务器比较卡,调用调用超时,响应很慢,成功率低的问题,然后想着去处理这个问题. 本着开发的精神,摒弃网管的水平,寻找问题的根源. 主要从如下几个方面入手: 1:查询 ...