当前位置:网站首页>leetcode:724. 寻找数组的中心下标
leetcode:724. 寻找数组的中心下标
2022-06-29 20:51:00 【OceanStar的学习笔记】
题目来源
题目描述


class Solution {
public:
int pivotIndex(vector<int>& nums) {
}
};
题目解析
暴力
思路:求下标i之前所有元素(不包括i)的和,以及下标i之后所有元素(不包括i)的和,比较二者是否相等,如果相等则表示i是中间位置,则返回i。如果所有都不能匹配则返回-1。
class Solution {
int beforeSum(vector<int>& nums, int idx){
int sum = 0;
for (int i = 0; i < idx; ++i) {
sum += nums[i];
}
return sum;
}
int afterSum(vector<int>& nums, int idx){
int sum = 0;
for (int i = nums.size() - 1; i > idx; --i) {
sum += nums[i];
}
return sum;
}
public:
int pivotIndex(vector<int>& nums) {
// 循环遍历数组中所有元素
for (int i = 0; i < nums.size(); ++i) {
// 计算下标i之前和之后所有元素的总和
int before = beforeSum(nums, i);
int after = afterSum(nums, i);
// 如果相等则返回i
if(before == after){
return i;
}
}
return -1;
}
};
前缀和
思路:如果某下标i的前缀和与其后缀和相等则表示找到了数组的中间位置。前缀和就是i之前所有元素的总和,后缀和就是i之后所有元素的总和,都不包括i
class Solution {
public:
int pivotIndex(vector<int>& nums) {
// 计算nums数组的中所有元素总和
int sum = 0;
for (int num : nums) {
sum += num;
}
int preSum = 0; // 前缀和
for (int i = 0; i < nums.size(); ++i) {
// 后缀和,即数组元素总和减去前缀和,减去当前元素得到的结果
int postSum = sum - preSum - nums[i];
if(preSum == postSum){
return i;
}
// 更新前缀和
preSum += nums[i];
}
return -1;
}
};
前缀和
- 因为: 前缀和 + nums[i] + 后缀和 = 总和
- 又:要求找到 【前缀和 == 后缀和】
- 所以: 2 * 前缀和 = 总和 - nums[i]
class Solution {
public:
int pivotIndex(vector<int>& nums) {
// 计算nums数组的中所有元素总和
int sum = 0;
for (int num : nums) {
sum += num;
}
int preSum = 0; // 前缀和
for (int i = 0; i < nums.size(); ++i) {
// 前缀和 + nums[i] + 后缀和 = 总和
// if(前缀和==后缀和) return i;
// 所以:if(2*前缀和=总和-nums[i]) return i;
if(2 * preSum == sum - nums[i]){
return i;
}
// 更新前缀和
preSum += nums[i];
}
return -1;
}
};
边栏推荐
- Community interview -- jumpserver open source fortress in the eyes of an it newcomer
- A Japanese Cherry sold at a sky high price of 1980 yuan. Netizen: I feel cheated after eating it
- "Xiaodeng" ad domain delegation for operation and maintenance
- 导航【微机原理】
- String类的常用方法
- go: 如何编写一个正确的udp服务端
- Advances in computational imaging
- In depth good article | yolov5+deepsort multi-target tracking in-depth interpretation and testing (including source code)
- How to use filters in jfinal to monitor Druid for SQL execution?
- 计算成像前沿进展
猜你喜欢

Selection of materials for space conductive disc slip ring

导航 实验【微机原理】【实验】
![Navigation exercises [microcomputer principles] [exercises]](/img/79/8311a409113331e72f650a83351b46.png)
Navigation exercises [microcomputer principles] [exercises]

.NetCore统一认证授权学习——第一次授权(2)

Hangfire详解

空间导电盘式滑环材料的选择

Uncover the secret! Pay attention to those machines under the membership system!

18. `bs对象.节点名.next_sibling` previous_sibling 获取兄弟节点

The reason why the log analysis tool of "operation and maintenance" is used more and more frequently

Win7 Easy Connect prompt: route selection connection failed. The current connection network may be abnormal. Please try again later
随机推荐
2021 CCPC Harbin J. local minimum (thinking question)
习近平在湖北武汉考察时强调 把科技的命脉牢牢掌握在自己手中 不断提升我国发展独立性自主性安全性
如何从外表判断导电滑环的质量
Analysis of the underlying architecture of spark storage system - spark business environment practice
Application of VoIP push in overseas audio and video services
Bigder: Automation Test Engineer
「运维有小邓」实时监控用户登录操作
Cantata version 9.5 has officially passed the sgs-t Ü V certification and conforms to all major software safety standards
Practical guide to GStreamer application development (V)
Exit operation in project
Codeforces Global Round 21 C D E
Initialization of global and static variables
Clock tree synthesis (CTS)
Liunx instruction
String类的常用方法
Proxmox cluster node crash handling
tmux设置
Golang basic learning
How do I audit Active Directory User account changes?
I found another cross domain method by chance. I don't know if anyone has ever played this way