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 ...
随机推荐
- 用eclipse来制作并使用可执行的jar文件
我近来用java写了一个股票收益分析的小程序,用于计算我的股票操作所带来的的收益.这里,记录了如何将源代码打包成可执行的命令的一个过程. 1:生成可执行的jar文件 选中工程,选择菜单中的export ...
- Android应用框架-Volley网络通信框架
1.Volley简介: Volley是Google 推出的 Android 异步网络请求框架和图片加载框架. 在 Google I/O 2013 大会上发布. 2.Volley特点 扩展性强. And ...
- 洛谷 P1312 Mayan游戏
题解:搜索+模拟 剪枝: 最优性剪枝:x从小到大,y从小到大,第一次搜到的就是字典序最小 的最优解. 最优性剪枝:把一个格子和左边格子交换,和左边格子和右边格 子交换是等价的,显然让左边格子和右边交换 ...
- FastAdmin 在线命令生成时出错的分析
FastAdmin 在线命令生成时出错的分析 出错现象 版本环境 FastAdmin 版本:1.0.0.20180806_beta 在线命令插件版本:1.0.3 分析 2018-08-13 16:12 ...
- poj 3590 The shuffle Problem——DP+置换
题目:http://poj.org/problem?id=3590 bzoj 1025 的弱化版.大概一样的 dp . 输出方案的时候小的环靠前.不用担心 dp 时用 > 还是 >= 来转 ...
- iptables防火墙工作原理
iptables防火墙工作原理 简介:iptables防火墙工作在网络层,针对TCP/IP数据包实施过滤和限制,iptables防火墙基于内核编码实现,具有非常稳定的性能和高效率: iptables属 ...
- 不常用的linux命令
不太常用的命令 vipw ##打开密码配置文件 dmesg ##补充说明:kernel会将开机信息存储在ring buffer中.您若是开机时来不及查看信息,可利用dme ...
- ClassLoader热加载的简单实现
当我们在eclipse中修改了一个.java文件时,并通过[ctrl + s ]保存了此java文件,相应的bin目录中,会发现.class文件也发生了修改.通常情况下,java文件是在我们的web项 ...
- 全文检索引擎Solr系列——整合中文分词组件IKAnalyzer
IK Analyzer是一款结合了词典和文法分析算法的中文分词组件,基于字符串匹配,支持用户词典扩展定义,支持细粒度和智能切分,比如: 张三说的确实在理 智能分词的结果是: 张三 | 说的 | 确实 ...
- PyYAML和configparser模块讲解
Python也可以很容易的处理ymal文档格式,只不过需要安装一个模块,参考文档:http://pyyaml.org/wiki/PyYAMLDocumentation ymal主要用于配置文件. Co ...