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 ...
 
随机推荐
- [POJ-3237] [Problem E]
			
Tree Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 13156 Accepted: 3358 题目链接 http: ...
 - Delphi XE7中使用JSON
			
Delphi XE7有一个对JSON处理的单元,在你需要使用JSON的单元里面引入"System.json",随后你就可以用Delphi自己的json处理类了.我写的小例子只是对包 ...
 - JavaWeb -JDBC使用(数据库-javaweb连接)
			
使用JDBC四个大类 1. DriverManager:管理jdbc驱动 2. Connection:连接(通过DriverManager产生) 3. Statement/PreparedStatem ...
 - BZOJ 1426--收集邮票(概率与期望&DP)
			
1426: 收集邮票 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 504 Solved: 417[Submit][Status][Discuss] ...
 - Linux命令行测试网速speedtest.net
			
Linux命令行测试网速speedtest.net 当发现上网速度变慢时,人们通常会先首先测试自己的电脑到网络服务提供商(通常被称为"最后一公里")的网络连接速度.在可用于测试宽带 ...
 - scrapy入门例子
			
使用爬取http://quotes.toscrape.com/内容,网站内容很简单 一. 使用scrapy创建项目 scrapy startproject myscrapy1 scrapy gensp ...
 - json和csv文件存储
			
一. json 1:基本概念 1.1 Json和Javascript JSON, 全称JavaScript Object Notation,它通过对象和数组的组合来表示数据.在JavaScript中一 ...
 - mac编辑器vim美化
			
mac编辑器vim美化 contents 环境 效果呈现 安装 quick start 环境 mac10.13.6,vim7(该版本mac自带的vim是7),git mac下vim的配置文件有两处 一 ...
 - .NET Core容器化之多容器应用部署-使用Docker-Compose
			
原文补充: -- docker-compose.ymlversion: ' services: mvc-web: container_name: mvc.web.compose build: . re ...
 - linux中pipe和dup2详解
			
1.什么是管道 管道是半双工的,数据只能向一个方向流动:需要双方通信时,需要建立起两个管道: 只能用于父子进程或者兄弟进程之间(具有亲缘关系的进程): 单独构成一种独立的文件系统:管道对于管道两端的进 ...