724. Find Pivot Index
Given an array of integers nums, write a method that returns the "pivot" index of this array.
We define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.
If no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.
Example 1:
Input:
nums = [1, 7, 3, 6, 5, 6]
Output: 3
Explanation:
The sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.
Also, 3 is the first index where this occurs.
Examlple 2:
Input:
nums = [1, 2, 3]
Output: -1
Explanation:
There is no index that satisfies the conditions in the problem statement.
给定一个数组,找出一个数,左边元素之和等于右边元素之和,存在范围其index,反之-1
假定存在这个数,满足:
- 1.left + nums[i] + right = sum
- 2.left = right
- 3.sum - 2 * left = nums[i]
public int pivotIndex(int[] nums) {
int sum = 0;
for(int num:nums)
sum += num;
int left=0;
for(int i = 0; i < nums.length; i++)
{ if (i != 0)
left += nums[i-1]; //确保是在‘pivot’的左侧
if(sum - 2 * left == nums[i]) return i;
}
return -1;
}
724. Find Pivot Index的更多相关文章
- 【Leetcode_easy】724. Find Pivot Index
problem 724. Find Pivot Index 题意:先求出数组的总和,然后维护一个当前数组之和curSum,然后对于遍历到的位置,用总和减去当前数字,看得到的结果是否是curSum的两倍 ...
- Python解Leetcode: 724. Find Pivot Index
leetcode 724. Find Pivot Index 题目描述:在数组中找到一个值,使得该值两边所有值的和相等.如果值存在,返回该值的索引,否则返回-1 思路:遍历两遍数组,第一遍求出数组的和 ...
- [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】724. Find Pivot Index 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和,再遍历 日期 题目地址:https://le ...
- [LeetCode] Find Pivot Index 寻找中枢点
Given an array of integers nums, write a method that returns the "pivot" index of this arr ...
- [Swift]LeetCode724. 寻找数组的中心索引 | 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_Easy tag: Dynamic Programming
Given an array of integers nums, write a method that returns the "pivot" index of this arr ...
- Array-Find Pivot Index
Given an array of integers nums, write a method that returns the "pivot" index of this arr ...
随机推荐
- unity插件开发
1.简单的svn集成: 查询svn的文档可以知道svn提供各种命令符操作.因此,原理非常简单,利用命令符操作调用svn即可.代码也非常简单: 更新:Process.Start("Tortoi ...
- php垃圾回收
php所有的变量都存在一个zval的结构里面,通过refcount和is_ref来存储变量的引用关系.refcount是变量的引用次数,is_ref是变量是否被引用,当is_ref=0的时候refco ...
- 尚未解决的selenium 定位
自从入职以来,一直在写selenium自动化脚本,可是最近因为一个问题止步不前.可是也不能一直原地踏步呀在这里把问题先记录一下,免得以后忘了. 前景: 做一个表单的提交,点击按钮,执行某函数,若表单中 ...
- 03.redis与ssm整合(mybatis二级缓存)
SSM+redis整合 ssm框架之前已经搭建过了,这里不再做代码复制工作. 这里主要是利用redis去做mybatis的二级缓存,mybaits映射文件中所有的select都会刷新已有缓存,如果不存 ...
- 51Nod 1001 数组中和等于K的数对 Set
给出一个整数K和一个无序数组A,A的元素为N个互不相同的整数,找出数组A中所有和等于K的数对.例如K = 8,数组A:{-1,6,5,3,4,2,9,0,8},所有和等于8的数对包括(-1,9),(0 ...
- 移动端H5制作安卓和IOS的坑 持续更新...
移动端H5制作安卓和IOS的坑 持续更新... 前言:最近参加公司的H5页面创意竞赛,又遇到不少页面在不同系统上的坑.踩坑之余,觉得很多之前遇到的知识点都忘了,索性开一篇博文,把这些坑都统一归纳起来, ...
- python自带库及第三方库api察看
今天发现一个很有意思的功能,python自带了所有库的文档查看器,配置如下: 配置pydoc服务,cmd中输入如下代码: python –m pydoc –p 1234 回车后 ,使用过程中,该窗口不 ...
- google guava cache缓存基本使用讲解
代码地址:https://github.com/vikde/demo-guava-cache 一.简介 guava cache是google guava中的一个内存缓存模块,用于将数据缓存到JVM内存 ...
- webpack loader加载器
配置loader,通过加载器处理文件,例如css sass less等,告诉webpack每一种文件都需要使用什么来加载器来处理. 1.node.js安装好之后也会自动默认安装好npm,所以cmd c ...
- object-fit?
知道有这个属性存在,是一个很偶然的机会.有一天,设计部的一个小伙伴给了我一个网址,说很有个性,让我看一下,当发现一个很有意思的效果时,作为一个前端小兵的我当然是第一时间开始审查元素,然后看到了这个从没 ...