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. vue 组件高级用法(递归组件,内联模板,动态组件,异步组件)

  2. MyBatis模糊查询和多条件查询

    一.ISmbmsUserDao层 //根据姓名模糊查询 public List<Smbms> getUser(); //多条件查询 public List<Smbms> get ...

  3. [RxJS] Convert a Node.js style callback to Observable: bindNodeCallback

    It's just like bindCallback, but the callback is expected to be of type callback(error, result). imp ...

  4. Spring AOP的作用,动态代理模式

    AOP即面向切面编程.AOP是基于代理模式的. 代理模式: 当我们需要修改一个类,在类中加入代码时,为了不破坏这个类的封装性.可以使用代理模式,建立一个代理类. 比如:修改需求,在调用UserCont ...

  5. ckeditor编辑器从word粘贴公式

    由于工作需要必须将word文档内容粘贴到编辑器中使用 但发现word中的图片粘贴后变成了file:///xxxx.jpg这种内容,如果上传到服务器后其他人也访问不了,网上找了很多编辑器发现没有一个能直 ...

  6. splay树 序列终结者

    /* 4655 序列终结者  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 大师 Master 题解       题目描述 Description 网上有许多题,就是给定一个序 ...

  7. 【説明する】hash

    首先对于判重,我们能想到的方法有什么呢? 1)bool数组 2)set(集) 数组与集合的优缺点: 1.因为集合是对数组做的封装,所以,数组永远比任何一个集合要快. 2.数组声明了它容纳的元素的类型, ...

  8. nestjs中typeorm进行事物操作

    https://typeorm.io/#/transactions 两种方法

  9. Python豆瓣书籍信息爬虫

    练习下BeautifulSoup,requests库,用python3.3 写了一个简易的豆瓣小爬虫,将爬取的信息在控制台输出并且写入文件中. 上源码: # coding = utf-8 '''my ...

  10. Tomcat单独部署,控制台乱码解决方法

    使用Tomcat作为容器,部署项目的时候,发现控制台总是乱码,乱码如下 打开Tomcat的部署目录 找到这一行,将UTF-8改成GBK java.util.logging.ConsoleHandler ...