724_Find-Pivot-Index
724_Find-Pivot-Index
Description
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.
Example 2:
Input:
nums = [1, 2, 3]
Output: -1
Explanation:
There is no index that satisfies the conditions in the problem statement.
Note:
The length of nums will be in the range [0, 10000].
Each element nums[i] will be an integer in the range [-1000, 1000].
Solution
Java solution
class Solution {
public int pivotIndex(int[] nums) {
int leftSum = 0, rightSum = 0;
for (int num : nums) {
rightSum += num;
}
for (int i=0; i<nums.length; i++) {
rightSum -= nums[i];
if (leftSum == rightSum) {
return i;
}
leftSum += nums[i];
}
return -1;
}
}
Runtime: 42 ms
Python solution
class Solution:
def pivotIndex(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
left_sum = 0
right_sum = sum(nums)
for i, x in enumerate(nums):
right_sum -= x
if left_sum == right_sum:
return i
left_sum += x
return -1
Runtime: 76 ms
724_Find-Pivot-Index的更多相关文章
- 724. Find Pivot Index
Given an array of integers nums, write a method that returns the "pivot" index of this arr ...
- [LeetCode] 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 ...
- [Swift]LeetCode724. 寻找数组的中心索引 | Find Pivot Index
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 ...
- 724. Find Pivot Index 找到中轴下标
[抄题]: Given an array of integers nums, write a method that returns the "pivot" index of th ...
- 【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 思路:遍历两遍数组,第一遍求出数组的和 ...
- C#LeetCode刷题之#724-寻找数组的中心索引( Find Pivot Index)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3742 访问. 给定一个整数类型的数组 nums,请编写一个能够返 ...
- 【LeetCode】724. Find Pivot Index 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和,再遍历 日期 题目地址:https://le ...
随机推荐
- SQL多行字符串按条件合并
USE [ARTEA.MES]GO /****** Object: UserDefinedFunction [dbo].[UnionPart] Script Date: 11/18/2015 15:3 ...
- 自己从0开始学习Unity的笔记 V (C#的数组练习)
今天练习了数组输入,先从最简单的开始,因为我输入完这些之后,觉得应该有更简单的方法,先来介绍一下我做的练习代码 //做一个最多能容纳10个数字的,用户可以输入任意1-10个数字,判断长度,输出数字 ] ...
- 函数IsValid()
功能:检查对象变量是否已经实例化,即实例变量的值是否是个有效的对象句柄. 语法:IsValid(objectname) 参数:objectname:要检查的对象名. 返回值:Boolean.如果指定对 ...
- 深入了解java虚拟机(JVM) 第十章 字节码指令
一.字节码指令的含义 Java字节码指令由一个字节长度的,代表某种特定操作含义的数字(操作码)以及其后的零至多个代表此操作所需参数(操作数).此外字节码指令是面向操作数栈的,这里操作数栈在功能上对应实 ...
- html中文字溢出处理(text-overflow)
文字溢出处理有两种方式: 一.css overflow:hidden; white-space: nowrap; text-overflow: ellips ...
- leetcode-278-First Bad Version(注意不要上溢)
题目描述:(说明中有简单翻译) You are a product manager and currently leading a team to develop a new product. Unf ...
- [BJOI2014]大融合(LCT)
题面 luogu bzoj是权限题.. 题解 \(LCT\)维护子树信息 因为\(LCT\)中有一些虚子树,\(splay\)维护不了. 所以要新开一个数组来记录 然后注意\(link\)时 是先\( ...
- 【Alpha】Phylab 发布说明
Phylab Alpha阶段发布说明 一.发布地址 Phylab 二.新功能 1. 控制台 由于往届项目控制台并未发布,因此我们在完善后将这部分放在新功能部分.目前使用控制台需要向开发者申请. 1.1 ...
- 使用go写一个简单的exe文件
工作需要一个小工具给分析师用,原先打算写一个脚本的,但是呢我又不会用python,要写的话只能用java来实现(打包成可执行jar,使用java -jar 的命令来执行,当然得安装jdk).这种命令行 ...
- WC2019退役记
sb题不会,暴力写不完,被全场吊着打,AFO