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 ...
随机推荐
- C#中索引器的作用和实现。
官方描述:索引器允许类或结构的实例就像数组一样进行索引.索引器形态类似于,不同之处在于它们的取值函数采用参数. 这一功能在创建集合类的场合特别有用,而在其他某些情况下,比如处理大型文件或者抽象有些资源 ...
- tcp udp socket编程
http://blog.csdn.net/ns_code/article/details/14128987
- Chrome 中删除单条浏览记录
悲伤...之前用非隐私窗口观看了小电影.于是打开 chrome://settings/ ...... 现在才知道 windows 上使用 shift + del 即可删除该浏览记录 ....... 以 ...
- SpringMvc渲染视图
这篇博文讨论的问题是从ModelAndView如何渲染到页面. 首先要知道每个请求处理完之后都会返回一个ModelAndView对象. 这里我分6种情况来分析,代表6种返回类型: ModelAndVi ...
- dubbo服务器启动后报错端口被占用
环境:maven工程,ssm框架,tomcat 情景:dubbo的服务注册方服务器启动 问题原因: 经过网络查找,结果是Root WebApplicationContext 启动了两次,第二次报错,d ...
- node-redis模块需要注意的事项
node之中连接redis使用的redis模块,虽然好用,但是有些地方还是需要注意. npm install redis redis client 行为:1.客户端执行过程中断网的情况 由于原本连接正 ...
- mysql 与sqlser group by
mysql select * ,count(1) from simccbillm18 group by MonthNum; SqlSer select col1,col2 from table g ...
- 进阶篇:4.3)DFA设计指南:宽松公差及人性装配及其他
本章目的:设计需要为装配考虑,给他们提供各种优待,装配才能做出好产品. 1.前言 机械贴合现实而软件远离现实. 越是学习机械设计的原则,越是感觉他们和一些做人做事的道理相同的. 如,机械设计原则都是有 ...
- 【性能测试】:JVM内存监控策略的方法,以及监控结果说明
JVM内存监控主要在稳定性压测期间,监控应用服务器内存泄露等问题: [JVM远程监控设置] 1.打开WAS控制台:https://ip:port/ibm/console/login.do 2.进入路径 ...
- Spring Boot中自动执行sql脚本
说明:所有的代码基于SpringBoot 2.0.3版本 背景 在应用程序启动后,可以自动执行建库.建表等SQL脚本.下文中以要自动化执行people.sql脚本为例说明,脚本在SpringBoot工 ...