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. 4.Python 进制和位运算

    .button, #logout { color: #333; background-color: #fff; border-color: #ccc; } span#login_widget > ...

  2. redis危险命令

    KEYS 单行遍历,速度很慢很占执行时间,对单核来说,极有可能导致执行完后处理不过来这段时间堆积的任务量,导致雪崩. FLUSHALL FLUSHDB CONFIG 今晚搜索kombu用的key,用了 ...

  3. python中的_xx, __xx, __xx__

    一.从模块分析 ########  bb.py (一个用来导入的模块) ########## var = 0_var = 1__var = 2__var__ = 3 1. from module im ...

  4. 16Vue 表单的输入绑定

    基础用法 你可以用 v-model 指令在表单 <input>.<textarea> 及 <select> 元素上创建双向数据绑定. 它会根据控件类型自动选取正确的 ...

  5. jquery显示隐藏密码跟显示密码

    今天讲述的是html5中input的password密码的加密与显示 都知道input标签加上password输入密码显示的都是原点.......怎么点一个按钮让他显示回来明文数字1234567 上代 ...

  6. Linux操作系统常用命令合集——第六篇-压缩和归档操作(16个命令)

    1.gzip [命令作用] 压缩和解压缩文件 gzip/guzip/zcat zcat:不显式展开的前提下查看文本文件内容 zdiff/zgrep/zless/zmore [命令语法]  gzip   ...

  7. 块状链表 codevs 2333弹飞绵羊

    块状链表,分块处理,先预处理每一个点跳到下一个块 跳到哪,步数.然后修改的时候,修该那一个块即可 #include<cstdio>#include<cmath>int a[20 ...

  8. 搭建自己的博客(二十二):通过ajax提交评论信息,并增加公式编辑功能

    编辑功能使用到了ckeditor的MathJax组件.ajax提交评论可以不用刷新浏览器. 1.变化的部分

  9. 爬虫(十四):scrapy下载中间件

    下载器中间件是介于Scrapy的request/response处理的钩子框架,是用于全局修改Scrapy request和response的一个轻量.底层的系统. 激活Downloader Midd ...

  10. 【原创】go语言学习(四)流程控制

    目录: 1.if else语句块 2.for语句 3.switch语句 if else语句块 1.基本语法 if condition { //do something } if statement; ...