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 ...
随机推荐
- mysql 查询整个数据库所有表的行数
>use information_schema; >select sum(table_rows) from tables where TABLE_SCHEMA = "test&q ...
- JAVA RPC (九) netty服务端解析
源码地址:https://gitee.com/a1234567891/koalas-rpc 企业生产级百亿日PV高可用可拓展的RPC框架.理论上并发数量接近服务器带宽,客户端采用thrift协议,服务 ...
- 基于Kafka+ELK搭建海量日志平台
早在传统的单体应用时代,查看日志大都通过SSH客户端登服务器去看,使用较多的命令就是 less 或者 tail.如果服务部署了好几台,就要分别登录到这几台机器上看,等到了分布式和微服务架构流行时代,一 ...
- FMX开源控件
FMX开源控件 这是群友谢顿做的控件,必须赞一个! https://github.com/zhaoyipeng/FMXComponents 这是loki的: https://sourceforge.n ...
- pycharm2019没有database问题(关于社区版)
原文链接:https://blog.csdn.net/BlacK_CaT_/article/details/53884806网上教程都是直接打开右上角的database,但是我死活也没找到,后来发现应 ...
- Android中利用jsoup解析html页面
学习jsoup :jsoup学习网站 Android 中使用: 添加依赖 implementation 'org.jsoup:jsoup:1.10.1' 直接上代码: package com.load ...
- DriverManager
1: 注册驱动 Class.forName("com.mysql.jdbc.Driver") ; static { try { java.sql.DriverManager.reg ...
- SQL查询交集、并集、差集
两个表A 和表 T 交集 intersect 并集 UNION SQL:select df from A union select sd from T; 在Union后不加A ...
- 小记LoadRunner 11 安装VC2005运行环境报错处理
这几天在做性能优化,需要在虚拟机里装个LoadRunner 11.从测试同学那里搞来安装包,按照文档提示安装系统运行环境,提示我要装VC2005 SP1. 安装程序自己安装,报错.截图如下. 于是我又 ...
- Anaconda+tensorflow(不用创建虚拟环境)
网上大部分教程都是:创建tensorflow虚拟环境(conda create -n tensorflow python=3.6),然后在虚拟环境中pip install tensorflow,但是每 ...