leetcode 724. Find Pivot Index

  • 题目描述:在数组中找到一个值,使得该值两边所有值的和相等。如果值存在,返回该值的索引,否则返回-1

  • 思路:遍历两遍数组,第一遍求出数组的和,第二遍开始,保存左边所有的值的和,当左边值的和的2倍加上当前值等于数组和时,就是要找的索引。时间复杂度为o(n),空间复杂度为o(1).

class Solution(object):
def pivotIndex(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
ret = sum(nums)
left = 0
for k, v in enumerate(nums):
if left * 2 + v == ret:
return k
left += v
return -1

Python解Leetcode: 724. Find Pivot Index的更多相关文章

  1. [Leetcode]724. Find Pivot Index

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

  2. 【Leetcode_easy】724. Find Pivot Index

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

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

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

  4. 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_Easy tag: Dynamic Programming

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

  6. 724. Find Pivot Index 找到中轴下标

    [抄题]: Given an array of integers nums, write a method that returns the "pivot" index of th ...

  7. Python 解LeetCode:Intersection of Two Arrays

    最近,在用解决LeetCode问题的时候,做了349: Intersection of Two Arrays这个问题,就是求两个列表的交集.我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写 ...

  8. Python 解leetcode:48. Rotate Image

    题目描述:把一个二维数组顺时针旋转90度: 思路: 对于数组每一圈进行旋转,使用m控制圈数: 每一圈的四个元素顺时针替换,可以直接使用Python的解包,使用k控制每一圈的具体元素: class So ...

  9. Python 解LeetCode:654. Maximum Binary Tree

    用一个整型数组构建一个二叉树,根结点是数组中的最大值,左右子树分别是根结点的值在数组中左右两边的部分. 分析,这是二叉树中比较容易想到的问题了,直接使用递归就行了,代码如下: class Soluti ...

随机推荐

  1. 自己实现dup2

    转自 http://blog.csdn.net/todd911/article/details/11747097 #include <stdio.h> #include <unist ...

  2. open, create, close

    1.open 系统调用 说明: 调用open函数打开或者创建一个文件.函数定义如下:  #include <fcntl.h> int open(const char *pathname, ...

  3. Zabbix+Grafana 展示示例1

    Zabbix+Grafana 展示示例 Grafana是一个跨平台的开源度量分析和可是化的工具,可以通过该将采集的数据查询然后可视化的展示,并及时通知. 1. Grafana 特性 1. 展示方式:快 ...

  4. SpringAOP配置与使用(示例)

    1.pom.xml追加 spring-aspects aspectjrt 为控制器以外的类织入切面 2.新建spring-aop.xml <?xml version="1.0" ...

  5. codeforces gym #101161E - ACM Tax(lca+主席树)

    题目链接: http://codeforces.com/gym/101161/attachments 题意: 给出节点数为$n$的树 有$q$次询问,输出$a$节点到$b$节点路程中,经过的边的中位数 ...

  6. MySQL二进制包安装

    mysql的安装有多种方法,这里就介绍一下二进制包安装. [root@node1 ~]# tar xvf mysql-5.7.27-linux-glibc2.12-x86_64.tar [root@n ...

  7. CF1053E Euler tour

    题意 给出一个某些位置不全的欧拉序,求出一个符合条件的,或输出不行 传送门 \(n \le 5*10^5\) 思路 终于不是一道神仙\(dp\) 变成了一道神仙构造 以下简称两相同数围成的是一个区间, ...

  8. Windows下的Jupyter Notebook 安装与自定义启动

    1.Jupyter Notebook 和 pip 为了更加方便地写 Python 代码,还需要安装 Jupyter notebook. 利用 pip 安装 Jupyter notebook. 为什么要 ...

  9. Add hyperlink to textblock wpf

    Add hyperlink to textblock wpf Displaying is rather simple, the navigation is another question. XAML ...

  10. 深入理解Android插件化技术

    深入理解Android插件化技术  转 https://zhuanlan.zhihu.com/p/33017826 插件化技术可以说是Android高级工程师所必须具备的技能之一,从2012年插件化概 ...