Python解Leetcode: 724. Find Pivot Index
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的更多相关文章
- [Leetcode]724. Find Pivot Index
Given an array of integers nums, write a method that returns the "pivot" index of this arr ...
- 【Leetcode_easy】724. Find Pivot Index
problem 724. Find Pivot Index 题意:先求出数组的总和,然后维护一个当前数组之和curSum,然后对于遍历到的位置,用总和减去当前数字,看得到的结果是否是curSum的两倍 ...
- 【LeetCode】724. Find Pivot Index 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 先求和,再遍历 日期 题目地址:https://le ...
- 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_Easy tag: Dynamic Programming
Given an array of integers nums, write a method that returns the "pivot" index of this arr ...
- 724. Find Pivot Index 找到中轴下标
[抄题]: Given an array of integers nums, write a method that returns the "pivot" index of th ...
- Python 解LeetCode:Intersection of Two Arrays
最近,在用解决LeetCode问题的时候,做了349: Intersection of Two Arrays这个问题,就是求两个列表的交集.我这种弱鸡,第一种想法是把问题解决,而不是分析复杂度,于是写 ...
- Python 解leetcode:48. Rotate Image
题目描述:把一个二维数组顺时针旋转90度: 思路: 对于数组每一圈进行旋转,使用m控制圈数: 每一圈的四个元素顺时针替换,可以直接使用Python的解包,使用k控制每一圈的具体元素: class So ...
- Python 解LeetCode:654. Maximum Binary Tree
用一个整型数组构建一个二叉树,根结点是数组中的最大值,左右子树分别是根结点的值在数组中左右两边的部分. 分析,这是二叉树中比较容易想到的问题了,直接使用递归就行了,代码如下: class Soluti ...
随机推荐
- P1594 护卫队
题目描述 护卫车队在一条单行的街道前排成一队,前面河上是一座单行的桥.因为街道是一条单行道,所以任何车辆都不能超车.桥能承受一个给定的最大承载量.为了控制桥上的交通,桥两边各站一个指挥员.护卫车队被分 ...
- kill函数
kill函数/命令产生信号 kill命令产生信号:kill -SIGKILL pid kill函数:给指定进程发送指定信号(不一定杀死) int kill(pid_t pid, int sig); ...
- springboot中web应用的统一异常处理
在web应用中,请求处理过程中发生异常是非常常见的情况.springboot为我们提供了一个默认的映射:/error,当处理中抛出异常之后,会转到该请求中处理,并且该请求有一个全局的错误页面用来展示异 ...
- ZOJ 2592 Think Positive ——(xjbg)
做法是,先求出前缀和pre.然后枚举端点i,[i+1,n]中pre最小的找出来,减去pre[i-1]大于0,这是第一个条件:第二个条件是,从i开始的后缀和和i之前的最小的一个pre相加大于0.只要满足 ...
- Selenium处理页面懒加载方法
在做selenium webdriver 在做UI自动化时,有些页面时使用懒加载的形式显示页面图片,如果在不向下移动滚动条时,获取到的图片会是网站的默认图片和真实的图片不相符. 1.滑动滚动条 1. ...
- 黑马vue---59-60、组件中的data和methods
黑马vue---59-60.组件中的data和methods 一.总结 一句话总结: 1. 组件可以有自己的 data 数据 2. 组件的 data 和 实例的 data 有点不一样,实例中的 dat ...
- arcgis python 标注
import arcpy mxd = arcpy.mapping.MapDocument("current") lyr = arcpy.mapping.ListLayers(mxd ...
- OLAP、OLTP的介绍CBO/RBO
OLTP与OLAP的介绍 数据处理大致可以分成两大类:联机事务处理OLTP(on-line transaction processing).联机分析处理OLAP(On-Line Analytical ...
- [go]template使用
//index.html {{if gt .Age 18}} <p>hello, old man, {{.Name}}</p> {{else}} <p>hello, ...
- 安装mysql问题解决
[root@apollo init.d]# /etc/init.d/mysqld startmy_print_defaults: Can't read dir of '/etc/my.cnf.' (E ...