[抄题]:

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. (六)java数据类型

      数据类型:决定了变量占据多大的空间,决定了变量存储什么类型的数据 整形: byte     1个字节 short    2个字节 int        4个字节 long     8个字节 浮点型 ...

  2. 【Javascrpt 速成篇】 一:js基础

     本系列文章Javascript一律简称js,javascript太长了((⊙﹏⊙)b) js概述 js是面向对象和基于事件驱动的解释型语言,主要用于WEB前端,处理用户交互.几年前js只是作为一种前 ...

  3. Windows下运行Hadoop

    Windows下运行Hadoop,通常有两种方式:一种是用VM方式安装一个Linux操作系统,这样基本可以实现全Linux环境的Hadoop运行:另一种是通过Cygwin模拟Linux环境.后者的好处 ...

  4. mysql 存储过程 事务处理 (转)

    BEGIN DECLARE t_error INTEGER DEFAULT 0;  DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1; S ...

  5. bzoj 4104 [Thu Summer Camp 2015]解密运算——思路

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4104 想了很久,想出一个 nlogn (也许是 n2logn )的,可惜空间是 n2 . 已 ...

  6. Amoeba mysql读写分离搭建及介绍

    Amoeba mysql读写分离搭建及介绍 推荐: http://blog.chinaunix.net/uid-20639775-id-154600.html

  7. java里的switch循环--你妹考试落榜了

    总结:switch循环,不用break.那么程序每一个case都会运行到,直到遇到break停止 package com.aa; //格子区域 //3行3列的格子 public class Bu { ...

  8. 1130 Infix Expression

    题意:给出一个语法树(二叉树),输出相应的中缀表达式. 思路:很显然,通过中序遍历来做.通过观察,发现除了根结点之外的所有非叶结点的两侧都要输出括号,故在中序遍历时判断一下即可. 代码: #inclu ...

  9. 1050 String Subtraction

    题意:给出两个字符串s1和s2,在s1中删去s2中含有的字符. 思路:注意,因为读入的字符串可能有空格,因此用C++的getline(cin,str).PAT系统迁移之后C语言中的gets()函数被禁 ...

  10. Dynamics CRM 2011 通过工作流发邮件时的权限问题

    场景: 在CRM中配置工作流,完成某个步骤后,发送邮件通知其他用户.发件人统一配置为管理员,收件人则根据业务需要设定动态值. 相关权限配置 首先启动流程的用户, 需要允许其他用户代表发送电子邮件 另外 ...