【Leetcode_easy】724. Find Pivot Index
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(), 0);//err..int curSum = 0;for(int i=0; i<nums.size(); ++i){if(sum-nums[i] == 2*curSum) return i;curSum += nums[i];}return -1;} };
参考
1. Leetcode_easy_724. Find Pivot Index;
2. Grandyang;
完
转载于:https://www.cnblogs.com/happyamyhope/p/11115129.html
【Leetcode_easy】724. Find Pivot Index相关推荐
- 724 Find Pivot Index
724 Find Pivot Index 利用数组前n项和 leftSum ==Sum -leftSum -num[i] class Solution {public int pivotIndex(i ...
- LeetCode 724. Find Pivot Index
Given an array of integers nums, write a method that returns the "pivot" index of this arr ...
- 【python】pycharm启动 一直index,无法运行
花了几个小时找相关资料,按网上提供答案操作,依然没有解决, 最后让它自动更新完才好(大概更新了十分钟 有固态情况) 参考链接 pycharm启动后总是不停的updating indices...ind ...
- 【es】Elasticsearch:inverted index,doc_values及source
文章目录 1.概述 2.Inverted index 3.Source 4.Doc_values 1.概述 转载:https://www.cnblogs.com/sanduzxcvbnm/p/1208 ...
- 【Elasticsearch】使用 Elasticsearch Freeze index API 创建冻结索引
1.概述 首先官网已经写得很清楚了 使用 Elasticsearch Freeze index API 创建冻结索引 下面说说怎么获取冻结的索引 GET _cat/indices 执行get后,获取所 ...
- 【Kafka】Error when freeing index buffer (kafka.log.OffsetIndex) NullPointException
1.美图 2.背景 今天windows启动kafka的时候,突然报错 2015-07-14 17:00:45,197] WARN Error when freeing index buffer (ka ...
- 【MySQL】性能优化之 Index Condition Pushdown
一 概念介绍 Index Condition Pushdown (ICP)是MySQL 5.6 版本中的新特性,是一种在存储引擎层使用索引过滤数据的一种优化方式. a 当关闭ICP时,inde ...
- 【Leetcode_easy】657. Robot Return to Origin
problem 657. Robot Return to Origin 题意: solution1: class Solution { public:bool judgeCircle(string m ...
- 【Leetcode_easy】821. Shortest Distance to a Character
problem 821. Shortest Distance to a Character 参考 1. Leetcode_easy_821. Shortest Distance to a Charac ...
- 【Elasticsearch】es 报错 index has not yet rolled over with that alias
1.概述 一个老的索引报错生命周期异常,如下 看起来是别名没有指向43索引,目前写索引最新的是51 PUT xxxx-xx-000043/_settings {"lifecycle" ...
最新文章
- scala中Stream理解
- (ios实战):retain,copy,assign及autorelease ,strong,weak
- 拽 Excel 到 ComponentOne C1FlexGrid
- python 高级面试题_Python高阶面试题
- Mac 录制视频,并转为GIF格式
- AgileConfig轻量级配置中心1.3.0发布,支持多用户权限控制
- H3C交换机设置DHCP中继,配合Linux 服务器为多VLAN提供DHCP地址分配服务
- java异常及错误处理大纲
- Shell 计算两个数的加减乘除取余(+、-、*、\、/)
- 视频超分中的自监督适应方案
- Latex调节目录间距\tableofcontents
- C语言库函数学习知识
- matlab 神经网络工具箱 nntraintool 详解
- 盘点AI国际顶级会议
- 程序员必读: 摸清Hash表的脾性
- MOOC 哈工大苏小红C语言 第七周练兵区——编程题
- 文化网,武汉文化网,湖北文化网——体制文化常识
- @Value注解从配置文件中读取数组
- [GoFrame学习] 报错 implement not found for interface IMenu, forgot register?
- 安卓 View 开发 绘制尺子
热门文章
- 拓端tecdat|R语言极值推断:广义帕累托分布GPD使用极大似然估计、轮廓似然估计、Delta法
- C++中对字符串操作
- ubuntua安装chrome_Ubuntu 16.04下安装64位谷歌Chrome浏览器
- python实现xgboost算法
- PPT:Semi-supervised Classification with Graph Convolutional Networks
- python os模块大全
- Django实战(一)——教育网站
- Servlet之间的跳转(MVC模式)
- C++Debug Assertion Failed!到底出错在哪里?
- 设某链表中最常用的操作是在链表的尾部_面试官:“双向链表”都不会,谁给你跳槽的勇气?...