当前位置:网站首页>leetcode:724. Find the central subscript of the array
leetcode:724. Find the central subscript of the array
2022-06-29 20:56:00 【Oceanstar's learning notes】
Title source
- leetcode:724. Find the central subscript of the array
- leetcode:1991. Find the central subscript of the array
Title Description


class Solution {
public:
int pivotIndex(vector<int>& nums) {
}
};
title
violence
Ideas : Find subscript i All previous elements ( barring i) And , And subscripts i And then all the elements ( barring i) And , Compare whether they are equal , If equal, it means i It's the middle position , Then return to i. If none of them match, it returns -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) {
// Loop through all elements in the array
for (int i = 0; i < nums.size(); ++i) {
// Calculate subscript i The sum of all elements before and after
int before = beforeSum(nums, i);
int after = afterSum(nums, i);
// If equal, return i
if(before == after){
return i;
}
}
return -1;
}
};
The prefix and
Ideas : If a subscript i If the prefix sum of is equal to its suffix sum, it means that the middle position of the array is found . The prefix and are i The sum of all previous elements , The suffix and are i Then the sum of all the elements , Not including i
class Solution {
public:
int pivotIndex(vector<int>& nums) {
// Calculation nums The sum of all elements in the array
int sum = 0;
for (int num : nums) {
sum += num;
}
int preSum = 0; // The prefix and
for (int i = 0; i < nums.size(); ++i) {
// Suffixes and , That is, the sum of array elements minus the prefix and , Subtract the current element to get the result
int postSum = sum - preSum - nums[i];
if(preSum == postSum){
return i;
}
// Update prefix and
preSum += nums[i];
}
return -1;
}
};
The prefix and
- because : The prefix and + nums[i] + Suffixes and = The sum of the
- also : Request to find 【 The prefix and == Suffixes and 】
- therefore : 2 * The prefix and = The sum of the - nums[i]
class Solution {
public:
int pivotIndex(vector<int>& nums) {
// Calculation nums The sum of all elements in the array
int sum = 0;
for (int num : nums) {
sum += num;
}
int preSum = 0; // The prefix and
for (int i = 0; i < nums.size(); ++i) {
// The prefix and + nums[i] + Suffixes and = The sum of the
// if( The prefix and == Suffixes and ) return i;
// therefore :if(2* The prefix and = The sum of the -nums[i]) return i;
if(2 * preSum == sum - nums[i]){
return i;
}
// Update prefix and
preSum += nums[i];
}
return -1;
}
};
边栏推荐
- leetcode:238. 除自身以外数组的乘积
- Application of VoIP push in overseas audio and video services
- Selection of materials for space conductive disc slip ring
- Flutter BottomNavigationBar带有页面切换示例
- Community interview -- jumpserver open source fortress in the eyes of an it newcomer
- Comparable comparator writing & ClassCastException class conversion exception
- Chainsafe cross chain bridge deployment tutorial
- 60 days of remote office experience sharing | community essay solicitation
- Analysis of the factors affecting the transmission signal of the conductive slip ring
- 「运维有小邓」审核并分析文件和文件夹访问权限
猜你喜欢

A Japanese Cherry sold at a sky high price of 1980 yuan. Netizen: I feel cheated after eating it

Practical guide to GStreamer application development (V)

Sentinel's quick start takes you through flow control in three minutes

How to evaluate iFLYTEK AI translation pen P20 series? Is it worth buying?

《强化学习周刊》第51期:PAC、ILQL、RRL&无模型强化学习集成于微电网络格控制:综述与启示

VHDL电话计费系统设计

leetcode:370. 区间加法

计算成像前沿进展

Exit operation in project

路由汇总带来的三层环路-解决实验
随机推荐
炒股开户请问哪个券商佣金是最低最安全的
实现inotify配合rsync实时备份
Recruit | DBA Data Engineer every week with an annual salary of 35+. Dream of Kyushu and bright stars!
About Effect Size
flutter BottomNavigationBar切换页面保持状态
LSF-bsub命令
高校如何基于云原生构建面向未来的智慧校园?全栈云原生VS传统技术架构
TMUX settings
VHDL电话计费系统设计
分析影响导电滑环传输信号的因素
Rsync 建立多目录模块的方法
解释PBR纹理贴图(texture-maps)
时钟树综合(CTS)
. NETCORE unified authentication authorization learning - run (1)
HAproxy + Keepalive实现LDAP代理服务
At least 3 years for learning amplifier?
管理人员应具备的基本素质
如何评价科大讯飞AI翻译笔P20系列,值得买吗?
Design of VHDL telephone billing system
High energy live broadcast, a gathering of celebrities! We invite you to explore bizdevops.