惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
Cisco Blogs
NISL@THU
NISL@THU
G
GRAHAM CLULEY
T
Threatpost
I
Intezer
D
Darknet – Hacking Tools, Hacker News & Cyber Security
P
Proofpoint News Feed
L
Lohrmann on Cybersecurity
Cisco Talos Blog
Cisco Talos Blog
P
Privacy & Cybersecurity Law Blog
Security Latest
Security Latest
P
Palo Alto Networks Blog
L
LINUX DO - 热门话题
Cyberwarzone
Cyberwarzone
AI
AI
Help Net Security
Help Net Security
Forbes - Security
Forbes - Security
T
The Exploit Database - CXSecurity.com
月光博客
月光博客
The GitHub Blog
The GitHub Blog
aimingoo的专栏
aimingoo的专栏
C
CERT Recently Published Vulnerability Notes
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
N
News and Events Feed by Topic
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Scott Helme
Scott Helme
A
About on SuperTechFans
N
Netflix TechBlog - Medium
TaoSecurity Blog
TaoSecurity Blog
V
V2EX
MongoDB | Blog
MongoDB | Blog
AWS News Blog
AWS News Blog
Google DeepMind News
Google DeepMind News
Google Online Security Blog
Google Online Security Blog
O
OpenAI News
Y
Y Combinator Blog
S
Securelist
GbyAI
GbyAI
D
Docker
SecWiki News
SecWiki News
The Hacker News
The Hacker News
有赞技术团队
有赞技术团队
T
Tenable Blog
WordPress大学
WordPress大学
S
SegmentFault 最新的问题
P
Privacy International News Feed
S
Security Affairs
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Hacker News - Newest:
Hacker News - Newest: "LLM"
H
Hackread – Cybersecurity News, Data Breaches, AI and More

Moon's Blog

Java内存 | Moon's Blog Spring Data JPA | Moon's Blog Spring Boot的Java Config | Moon's Blog Java知识点 | Moon's Blog Spring Boot知识点 | Moon's Blog Java异常机制 | Moon's Blog Java的类加载器 | Moon's Blog JVM类加载机制 | Moon's Blog 单体架构与微服务架构 | Moon's Blog 424.替换后的最长重复字符 | Moon's Blog 888.公平的糖果棒交换 | Moon's Blog 839.相似字符串组 | Moon's Blog 778.水位上升的泳池中游泳 | Moon's Blog 1631.最小体力消耗路径 | Moon's Blog 使用Jackson库实现日期序列化 | Moon's Blog 使用Jackson库实现Java多态解析 | Moon's Blog Spring Boot+InfluxDB实现日志管理 | Moon's Blog Java多态+工厂模式实现服务调用 | Moon's Blog 为Spring Boot项目生成OpenAPI3.0文档 | Moon's Blog
724.寻找数组的中心索引 | Moon's Blog
Moon Lou · 2021-01-29 · via Moon's Blog

724.寻找数组的中心索引

题目

给你一个整数数组 nums,请编写一个能够返回数组 “中心索引” 的方法。

数组 中心索引 是数组的一个索引,其左侧所有元素相加的和等于右侧所有元素相加的和。

如果数组不存在中心索引,返回 -1 。如果数组有多个中心索引,应该返回最靠近左边的那一个。

注意:中心索引可能出现在数组的两端。

示例 1:

输入:nums = [1, 7, 3, 6, 5, 6]
输出:3
解释:
索引 3 (nums[3] = 6) 的左侧数之和 (1 + 7 + 3 = 11),与右侧数之和 (5 + 6 = 11) 相等。
同时, 3 也是第一个符合要求的中心索引。

示例 2:

输入:nums = [1, 2, 3]
输出:-1
解释:
数组中不存在满足此条件的中心索引。

示例 3:

输入:nums = [2, 1, -1]
输出:0
解释:
索引 0 左侧不存在元素,视作和为 0 ;右侧数之和为 1 + (-1) = 0 ,二者相等。

示例 4:

输入:nums = [0, 0, 0, 0, 1]
输出:4
解释:
索引 4 左侧数之和为 0 ;右侧不存在元素,视作和为 0 ,二者相等。

提示:

  • nums 的长度范围为 [0, 10000]。
  • 任何一个 nums[i] 将会是一个范围在 [-1000, 1000]的整数。

思路

首先思路肯定是遍历数组,找到符合条件的中间索引。

不动脑筋地想一下,每遍历到一个数,都要累加其左边的所有数,再累加其后边的所有数,然后比较是否相等。这样的时间复杂度是n方,肯定是超时的。

上面方法的核心问题在于,在累加时做了很多冗余的计算

基于以上方法的改进思路:首先数组的总和是确定的,那么累加了左边所有数,直接有:总和-左边和-当前数=右边和,这样就省去了累加右边和的计算;其次每次迭代到一个数,不需要逐个累加左边数,因为迭代是从左到右顺序进行的,只需要用上个迭代的左边和,加上上个迭代的数,就是这个迭代的左边和,因此每个迭代只需要加一次就行了。最终改进后的时间复杂度是线性的

代码

class Solution {
public int pivotIndex(int[] nums) {
int sum = 0;
for (int i : nums) {
sum = sum + i;
}
int left = 0;
for (int i=0; i<nums.length; ++i) {
if ((2*left + nums[i]) == sum) {
return i;
} else {
left = left + nums[i];
}
}
return -1;
}
}

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Moon's Blog