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.
[暴力解法]:
leftsum, rightsum都用for循环来求
时间分析:n+n
空间分析:
[优化后]:求leftsum, 剩下的用减法判断sum - nums[i] - leftsum 是否等于leftsum
时间分析:n+1
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
还是要有点自信的,有时候真的就只能用暴力解法
[一句话思路]:
不嵌套 一次只加一个数、化加法为减法,降低时间复杂度
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 提前标注一下特殊的 不是i的 边界值:leftsum只加到了i - 1
[二刷]:
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
不嵌套 一次只加一个数、化加法为减法,降低时间复杂度
[复杂度]:Time complexity: O(n) Space complexity: O(1)
[英文数据结构或算法,为什么不用别的数据结构或算法]:
[关键模板化代码]:
降低复杂度:
for (int i = 0; i < nums.length; i++) {
if (i != 0) leftsum += nums[i - 1];
if ((sum - leftsum - nums[i]) == leftsum) return i;
}
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
class Solution {
public int pivotIndex(int[] nums) {
//cc
if (nums == null || nums.length == 0) {
return -1;
}
//ini
int leftsum = 0, sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
}
//for loop, leftsum to i - 1, deduce
for (int i = 0; i < nums.length; i++) {
if (i != 0) leftsum += nums[i - 1];
if ((sum - leftsum - nums[i]) == leftsum) return i;
}
return -1;
}
}
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的两倍 ...
- 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
Given an array of integers nums, write a method that returns the "pivot" index of this arr ...
- 【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 ...
随机推荐
- [QT]问题记录-控件初始化导致程序异常关闭
qt新手,在设置 pushButton 的字体颜色时,出现软件异常闭,代码如下: 按钮的初始化在 ui->setupUi(this); 前边,会出现一下问题. 解决办法:将按钮的初始化在 u ...
- 【Python系统学习】基础篇
这次真的是最后一次了!第三次滚Python的基础.走了太多弯路.认真一点!菜鸟! 教程 转义字符 \ 可以转义很多字符,比如\n表示换行,\t表示制表符,字符\本身也要转义,所以\\表示的字符就是\ ...
- 中 varStatus的属性简介
varStatus是<c:forEach>jstl循环标签的一个属性,varStatus属性.就拿varStatus="status"来说,事实上定义了一个status ...
- ASP.NET的几个试题(《C#与.NET程序员面试宝典》)
更多参考:博客园笔记 :ASP.NET是什么 ASP.NET不是一种语言,而是创建动态Web页的一种强大的服务器端技术,它是Microsoft.NET Framework中一套用于生成Web应用程序和 ...
- [ArgumentException: 可能证书“CN=JRNet01-PC”没有能够进行密钥交换的私钥,或者进程可能没有访问私钥的权限。有关详细信息,请参见内部异常。]
堆栈跟踪: [CryptographicException: 密钥集不存在. ] System.Security.Cryptography.Utils.CreateProvHandle(CspPara ...
- 更新pip
python -m pip install pip --upgrade pip要保持最新的,才可以去下载最新的其他的第三方包
- winSCP连接FTP没有上传的权限
错误: 原因: ftp用户为 1)查看ubantu中FTP文件夹目录所有者及权限,发现ftpName用户对FTP文件夹的权限为 “r-x” ,仅有读,执行权限 2) chmod o=rwx ftp ...
- swift 学习-- 元组
//元组 //定义:元组是有多个值组合而成的复合值,其中的值可以是任意类型,而且每一个元素的类型可以是不同的 let http404Error = (404, "Not Found" ...
- [翻译]Web开发牛人访谈:你们都在用什么?
小肥鱼译注:早上看到这篇文章,觉得内容甚是有趣.作者跟web开发方面的诸多大牛进行了交流,了解到他们的研究动向,从访谈中可以看到各种风格的开发者,有浏览器控,有设备控.我想,知道行业里的优秀成员在做些 ...
- PMODB GT202 WIFI的使用
pHTTPSession = (P_HTTP_SESSION)malloc(ALIGN(sizeof(HTTP_SESSION))) PMODA/PMODB都可做WIFI使用,现介绍PMODB WIF ...