[抄题]:

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]:

[思维问题]:

还是要有点自信的,有时候真的就只能用暴力解法

[一句话思路]:

不嵌套 一次只加一个数、化加法为减法,降低时间复杂度

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 提前标注一下特殊的 不是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 找到中轴下标的更多相关文章

  1. Python解Leetcode: 724. Find Pivot Index

    leetcode 724. Find Pivot Index 题目描述:在数组中找到一个值,使得该值两边所有值的和相等.如果值存在,返回该值的索引,否则返回-1 思路:遍历两遍数组,第一遍求出数组的和 ...

  2. 【Leetcode_easy】724. Find Pivot Index

    problem 724. Find Pivot Index 题意:先求出数组的总和,然后维护一个当前数组之和curSum,然后对于遍历到的位置,用总和减去当前数字,看得到的结果是否是curSum的两倍 ...

  3. 724. Find Pivot Index

    Given an array of integers nums, write a method that returns the "pivot" index of this arr ...

  4. [Leetcode]724. Find Pivot Index

    Given an array of integers nums, write a method that returns the "pivot" index of this arr ...

  5. 【LeetCode】724. Find Pivot Index 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和,再遍历 日期 题目地址:https://le ...

  6. [LeetCode] Find Pivot Index 寻找中枢点

    Given an array of integers nums, write a method that returns the "pivot" index of this arr ...

  7. [Swift]LeetCode724. 寻找数组的中心索引 | Find Pivot Index

    Given an array of integers nums, write a method that returns the "pivot" index of this arr ...

  8. [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 ...

  9. Array-Find Pivot Index

    Given an array of integers nums, write a method that returns the "pivot" index of this arr ...

随机推荐

  1. kali视频(16-20)学习

    第五周 kali视频(16-20)学习 16.漏洞分析之数据库评估(一) 17.漏洞分析之数据库评估(二) 18.漏洞分析之WEB应用代理 19.漏洞分析之burpsuite 20.漏洞分析之fuzz ...

  2. opencv 学习笔记集锦

    整理了我所了解的有关OpenCV的学习笔记.原理分析.使用例程等相关的博文.排序不分先后,随机整理的.如果有好的资源,也欢迎介绍和分享. 1:OpenCV学习笔记 作者:CSDN数量:55篇博文网址: ...

  3. Centos7下命令笔记-ls

    ls命令大概是linux下最常用的命令之一,ls是list的缩写.因为linux目录或者文件记录的信息实在太多,所以默认ls只显示非隐藏的目录以及文件名.ls直接执行不加参数时显示本目录下的档案名. ...

  4. LG4360 [CEOI2004]锯木厂选址

    题意 原题来自:CEOI 2004 从山顶上到山底下沿着一条直线种植了 n 棵老树.当地的政府决定把他们砍下来.为了不浪费任何一棵木材,树被砍倒后要运送到锯木厂. 木材只能朝山下运.山脚下有一个锯木厂 ...

  5. Maven依赖调解

    引用来自maven实战中的一段话.

  6. ecshop其他页面判断是智能手机访问也跳转到ECTouch对应手机版页面(转)

    ecshop 其他页面(商品详情页.商品分类页.团购页.优惠活动页.积分商城) 判断如果是智能手机访问跳转到ECTouch1.0手机版对应页面 方法 首先在ecshop 根目录下 includes/l ...

  7. CF 1088(A , B , C , D)——思路

    http://codeforces.com/contest/1088 A:Ehab and another construction problem 输出 2 和 n(偶数的话)或者 2 和 n-1( ...

  8. dx工具(android将jar包转成dex格式二进制jar包工具)

    博客分类: android 时钟 dx工具二进制jar包  好吧,不得不承认这个工具真心难找,也不知道自己sdk里以前怎么就有了,还好给了师傅一份,现在重装系统从网上找这个工具都找不到. 将platf ...

  9. android中HttpClient的应用(POST方法)

    首先在http://hc.apache.org/downloads.cgi下载HttpClient包 直接看代码 import android.os.Handler; import android.o ...

  10. LVS+Keepalived搭建

    LVS+Keepalived搭建 原理说明 (推荐): http://www.cnblogs.com/likehua/archive/2014/06/19/3796849.html http://ou ...