[Leetcode]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.
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
numswill be in the range[0, 10000]. - Each element
nums[i]will be an integer in the range[-1000, 1000].
class Solution {
public int pivotIndex(int[] nums) {
if (nums.length <= 0)
return -1;
int pre = 0, post = 0;
for (int i = 0; i < nums.length; i++)
post += nums[i];
for (int i = 0; i < nums.length; i++){
post -= nums[i];
if (pre == post)
return i;
pre += nums[i];
}
return -1;
}
}
[Leetcode]724. Find Pivot Index的更多相关文章
- Python解Leetcode: 724. Find Pivot Index
leetcode 724. Find Pivot Index 题目描述:在数组中找到一个值,使得该值两边所有值的和相等.如果值存在,返回该值的索引,否则返回-1 思路:遍历两遍数组,第一遍求出数组的和 ...
- 【Leetcode_easy】724. Find Pivot Index
problem 724. Find Pivot Index 题意:先求出数组的总和,然后维护一个当前数组之和curSum,然后对于遍历到的位置,用总和减去当前数字,看得到的结果是否是curSum的两倍 ...
- 【LeetCode】724. Find Pivot Index 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和,再遍历 日期 题目地址:https://le ...
- 724. 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 ...
- 724. Find Pivot Index 找到中轴下标
[抄题]: Given an array of integers nums, write a method that returns the "pivot" index of th ...
- [LeetCode] Find Pivot Index 寻找中枢点
Given an array of integers nums, write a method that returns the "pivot" index of this arr ...
- C#LeetCode刷题之#724-寻找数组的中心索引( Find Pivot Index)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3742 访问. 给定一个整数类型的数组 nums,请编写一个能够返 ...
- [Swift]LeetCode724. 寻找数组的中心索引 | Find Pivot Index
Given an array of integers nums, write a method that returns the "pivot" index of this arr ...
随机推荐
- The more,the better。
贪婪是好的, 贪婪是对的, 贪婪是有用的, 贪婪是可以清理一切的, 贪婪是不断进化和进步的精华所在, 贪婪就是一切形式所在: 对于生活,对于爱情,对于知识我们一定要贪婪, 贪婪就是人们的动力. The ...
- 团队项目第二阶段个人进展——Day10
一.昨天工作总结 冲刺第十天,做程序的测试,并修复一些小的bug 二.遇到的问题 无 三.今日工作规划 继续对程序进行测试优化
- Python_替换当前目录下文件类型
''' 将当前目录的所有扩展名为html的文件重命名为扩展名为htm的文件 方法一 ''' import os file_list=os.listdir('.') for filename in fi ...
- echarts3 迁徙图 迁入迁出
geoCoordMap = { '上海': [121.4648,31.2891], '佛山': [112.8955,23.1097], '保定': [115.0488,39.0948], '兰州': ...
- capwap学习笔记——初识capwap(二)(转)
2.5.1 AC发现机制 WTP使用AC发现机制来得知哪些AC是可用的,决定最佳的AC来建立CAPWAP连接. WTP的发现过程是可选的.如果在WTP上静态配置了AC,那么WTP并不需要完成AC的发现 ...
- 并发库应用之十三 & 并发集合类的应用
传统集合实现同步的问题 举了一个例子:Map集合线程不同步导致的问题. 解决办法:使用同步的Map集合 使用集合工具类中的方法将不同步的集合转为同步的Collections.synchronizedM ...
- 并行(Parallelism)与并发(Concurrency)
并行(Parallelism):多任务在同一时刻运行.例如,多个任务在多核处理器上运行. 并发(Concurrency):两个或者两个以上的任务在一段时间内开始.运行.完成,这意味着它们不是在同一时刻 ...
- Fedora Linux中解决“xxx不在sudoers文件中”
问题描述: 在Fedora中执行一些操作时需要使用root权限,当我使用命令: sudo 想在普通用户中临时获得root权限时,却被提示: "xxx 不在 sudoers 文件中.此事将被报 ...
- 在Python中用Request库模拟登录(四):哔哩哔哩(有加密,有验证码)
!已失效! 抓包分析 获取验证码 获取加密公钥 其中hash是变化的,公钥key不变 登录 其中用户名没有被加密,密码被加密. 因为在获取公钥的时候同时返回了一个hash值,推测此hash值与密码加密 ...
- Volley 图片加载相关源码解析
转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/47721631: 本文出自:[张鸿洋的博客] 一 概述 最近在完善图片加载方面的 ...