【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相同, ...
随机推荐
- UML类图介绍
一.概述 UML类图用来定义系统中的类,包括描述类的结构和类之间的关系.类图的主要作用于描述系统的静态结构. 类图的基本模型元素如下:
- javax.jms.JMSException: Failed to build body from content. Serializable class not available to broker. Reason: java.lang.ClassNotFoundException: Forbidden class com.javaliao.portal.model.TbLogVisit! T
javax.jms.jmsexception:无法从内容生成正文.可序列化类不可用于代理原因:java.lang.ClassNotFoundException: 禁止类com.javaliao.por ...
- Java原子类--AtomicLongArray
转载请注明出处:http://www.cnblogs.com/skywang12345/p/3514604.html AtomicLongArray介绍和函数列表 在"Java多线程系列-- ...
- 2019牛客暑期多校训练营(第九场)All men are brothers——并查集&&组合数
题意 最初有 $n$ 个人且互不认识,接下来 $m$ 行,每行有 $x,y$,表示 $x$ 和 $y$ 交朋友,朋友关系满足自反性和传递性,每次输出当前选取4个人且互不认识的方案数. 分析 并查集维护 ...
- Appium自动化测试教程-自学网-monkey自定义脚本实践
自定义脚本的稳定性测试 常规Monkey测试执行的是随机的事件流,但如果只是想让Monkey测试某个特定场景这时候就需要用到自定义脚本了,Monkey支持执行用户自定义脚本的测试,用户只需要按照Mon ...
- JZOJ 5870 地图
直接解释题解,记录一下.
- mongodb 开发规范
一.命名规则 1.数据库命名规则 数据库名可以是满足以下条件的任意UTF-8字符串: (1)不能是空字符串(”") : (2)不能含有”(空格)...$./..和(空字符): (3)应全部小 ...
- luogu 2152
SuperGcd 二进制算法 1. A = B, Gcd(A, B) = A; 2. A,B为偶数, Gcd(A, B) = 2 * Gcd(A / 2, B / 2); 3. A 为偶数, B 为 ...
- Other-Website-Contents.md
title: 本站目录 categories: Other sticky: 10 toc: true keywords: 机器学习基础 深度学习基础 人工智能数学知识 机器学习入门 date: 999 ...
- 前端武器库之DOM练习
1.模态对话框 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...