【Leetcode_easy】724. Find Pivot Index
problem
题意:先求出数组的总和,然后维护一个当前数组之和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的更多相关文章
- 【LeetCode】724. Find Pivot Index 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和,再遍历 日期 题目地址:https://le ...
- 1229【MySQL】性能优化之 Index Condition Pushdown
转自http://blog.itpub.net/22664653/viewspace-1210844/ [MySQL]性能优化之 Index Condition Pushdown2014-07-06 ...
- Python解Leetcode: 724. Find Pivot Index
leetcode 724. Find Pivot Index 题目描述:在数组中找到一个值,使得该值两边所有值的和相等.如果值存在,返回该值的索引,否则返回-1 思路:遍历两遍数组,第一遍求出数组的和 ...
- 【C#】【MySQL】【GridView】删除出现Parameter index is out of range
[编程语言]C# [数据库]MySQL [控件]GridView [问题描述]GridView控件中自带[删除],[编辑],[选择],三个按钮[编辑],[选择]正常使用,但是在使用删除时,却报错Par ...
- 724. Find Pivot Index
Given an array of integers nums, write a method that returns the "pivot" index of this arr ...
- [Leetcode]724. Find Pivot Index
Given an array of integers nums, write a method that returns the "pivot" index of this arr ...
- 724. Find Pivot Index 找到中轴下标
[抄题]: Given an array of integers nums, write a method that returns the "pivot" index of th ...
- 【Leetcode_easy】852. Peak Index in a Mountain Array
problem 852. Peak Index in a Mountain Array solution1: class Solution { public: int peakIndexInMount ...
- 【Leetcode_easy】599. Minimum Index Sum of Two Lists
problem 599. Minimum Index Sum of Two Lists 题意:给出两个字符串数组,找到坐标位置之和最小的相同的字符串. 计算两个的坐标之和,如果与最小坐标和sum相同, ...
随机推荐
- appium+python+iOS 环境搭建,安装fbsimctl
(1)设置postBuffer的值 开始如下的安装之前,需先修改postBuffer的值,默认的比较下,会导致下载大文件时失败, 参考链接:http://blog.sina.com.cn/s/blog ...
- 清除MSSQL历史记录
declare @dt datetime select @dt = cast(N'2019-05-21T14:13:45' as datetime) exec msdb.dbo.sp_delete_b ...
- 函数指针,使用qsort,进行结构体排序
#include <stdio.h> #include <stdlib.h> #define STU_NAME_LEN 16 /*学生信息*/ typedef struct s ...
- UWB DWM1000 开源项目框架
UWB 目前比较火热,不论国内还是国外目前都掀起一股热潮. 但是实际工程代码很少,开源代码更少. 目前代码主要有 1 DecaWave Release的定位源码,代码基于TWR,一个非常大的状态机. ...
- HTML div块内剧中
在HTML中有一个块内元素剧中的方法 那就是margin:0 auto; 剧中前 剧中后
- html5文件夹上传源码
前段时间做视频上传业务,通过网页上传视频到服务器. 视频大小 小则几十M,大则 1G+,以一般的HTTP请求发送数据的方式的话,会遇到的问题:1,文件过大,超出服务端的请求大小限制:2,请求时间过长, ...
- (转)glances用法
借鉴:https://www.ibm.com/developerworks/cn/linux/1304_caoyq_glances/index.html glances 可以为 Unix 和 Linu ...
- 如果项目在IIS发布后,出现System.ComponentModel.Win32Exception: 拒绝访问。
如果项目在IIS发布后,出现System.ComponentModel.Win32Exception: 拒绝访问. 那么就试试下面的办法. 步骤如下: 应用程序池=>设置应用程序池默认设置 将标 ...
- 开发WINDOWS服务程序
开发WINDOWS服务程序 开发步骤: 1.New->Other->Service Application 2.现在一个服务程序的框架已经搭起来了,打开Service1窗口,有几个属性说明 ...
- oracle中的trigger
https://blog.csdn.net/indexman/article/details/8023740/ https://www.cnblogs.com/sharpest/p/7764660.h ...