当前位置:网站首页>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;
}
};
边栏推荐
- VHDL电话计费系统设计
- 路由汇总带来的三层环路-解决实验
- STL教程6-deque、stack、queue、list容器
- Calibration, correction and world coordinate calculation of binocular stereo vision camera (openCV)
- 直播预告 | PostgreSQL 内核解读系列第一讲:PostgreSQL 系统概述
- How do I audit Active Directory User account changes?
- 炒股开户请问哪个券商佣金是最低最安全的
- verilog实现串口通信发送到数码管
- GoAhead 翻译—Active Server Pages
- Website stress testing tool - webbench
猜你喜欢

ads131a04 ADC verilog实现及仿真

STM32最小系统搭建(原理图)

"Xiaodeng" active directory batch user creation in operation and maintenance
【云原生实战】KubeSphere实战——多租户系统实战

数字密码锁verilog设计+仿真+上板验证

Live broadcast preview | PostgreSQL kernel Interpretation Series Lecture 1: overview of PostgreSQL system

CORDIC based Signal Processor desgn

高校如何基于云原生构建面向未来的智慧校园?全栈云原生VS传统技术架构

「运维有小邓」Active Directory批量用户创建

PostgreSQL Weekly News - 22 juin
随机推荐
In depth good article | yolov5+deepsort multi-target tracking in-depth interpretation and testing (including source code)
PostgreSQL Weekly News - 22 juin
如何在 SAP BTP ABAP 编程环境里直接调用 ABAP On-Premises 系统的 RFC 函数
How can colleges and universities build future oriented smart campus based on cloud native? Full stack cloud native vs traditional technology architecture
Set up your own website (12)
Coreldraw2022 new version v24.1.0.360 update
Digital password lock Verilog design + simulation + on board verification
Stm32cubemx learning (6) external interrupt experiment
智能门锁主流品牌有哪些?选购门锁时要注重产品的哪些特性?
《强化学习周刊》第51期:PAC、ILQL、RRL&无模型强化学习集成于微电网络格控制:综述与启示
LSF-bsub命令
双目立体视觉摄像头的标定、矫正、世界坐标计算(opencv)
阿里云发布《中国机器人产业图谱(2022)》,122页pdf
透过华为军团看科技之变(五):智慧园区
"Xiaodeng" active directory password expiration notification function is available for operation and maintenance
I found another cross domain method by chance. I don't know if anyone has ever played this way
数字密码锁verilog设计+仿真+上板验证
Exit operation in project
如何审核 Active Directory 用户账户更改?
LSF bsub command