LeetCode.985-查询后偶数的总和(Sum of Even Numbers After Queries)

MATLAB 控制 E4438C 信号源的研究 通过 MATLAB,我们可以实现对 E4438C 的远程控制,从而实现更高的自动化和可编程性。总结起来,通过使用 MATLAB 和 Instrument Control Toolbox,我们可以轻松地控制 E4438C 信号源并生成各种类型的信号。需要注意的是,以上代码仅为示例,实际应用中可能需要根据具体情况进行更详细的配置和错误处理。此外,MATLAB 还提供了一系列函数和命令,可用于控制 E4438C 信号源的其他功能,如频率调谐、功率设置、调制设置等。您可以根据自己的需求进行相应的配置和控制。 阅读详情

这是悦乐书的第370次更新,第398篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第232题(顺位题号是985)。有一个整数数组A和一个查询数组queries。

对于第i个查询val = queries[i][0],index = queries[i][1],我们将val添加到A[index]。然后,第i个查询的答案是A的偶数值的总和。(这里给定的index = queries[i][1]是一个基于0的索引,每个查询都会修改数组A。)

返回所有查询的答案。你的答案数组answer应该是answer[i]作为第i个查询的答案。例如:

输入:A = [1,2,3,4],queries = [[1,0],[-3,1],[-4,0],[2,3]]
输出:[8,6,2,4]
说明:一开始,数组是[1,2,3,4]。
在向A[0]加1后,数组为[2,2,3,4],偶数值之和为2 + 2 + 4 = 8。
在向A[1]加-3后,数组为[2,-1,3,4],偶数值之和为2 + 4 = 6。
在向A[0]加上-4后,数组为[-2,-1,3,4],偶数值之和为-2 + 4 = 2。
在向A[3]加2后,数组为[-2,-1,3,6],偶数值之和为-2 + 6 = 4。

注意:

  • 1 <= A.length <= 10000

  • -10000 <= A[i] <= 10000

  • 1 <= queries.length <= 10000

  • -10000 <= queries[i][0] <= 10000

  • 0 <= queries[i][1] <A.length

02 第一种解法

直接翻译题目,找出queries中对应的索引和val,改变A中对应元素的的值,接着利用循环求A中偶数元素的总和,赋值给结果数组result。

此解法的时间复杂度为O(M*N),M为queries的长度,N为A的长度,空间复杂度为O(M),M为queries的长度。

public int[] sumEvenAfterQueries(int[] A, int[][] queries) {
    int len = queries.length;
    int[] result = new int[len];
    for (int i=0; i<len; i++) {
        A[queries[i][1]] += queries[i][0];
        result[i] = evenSum(A);
    }
    return result;
}

public int evenSum(int[] A){
    int sum = 0;
    for (int num : A) {
        if (num%2==0) {
            sum += num;    
        }
    }
    return sum;
}

03 第二种解法

第一种解法的时间复杂度太高了,还可以再优化下。

结果数组中的值,后一位是在前一位的基础上产生的,主要判断A中的当前位元素值、queries中的值的奇偶性。拿题目给的示例来看,在第二次操作后,A变成了[2,-1,3,4],将第一次操作后的数组A=[2,2,3,4]中的第二个元素变成了-1,而第一次操作的偶数元素和是8,现在第二个元素变成了奇数,就需要将第一次加的2给减掉,所以第二次操作后的偶数和变成了8-2=6,剩下的几步操作都可以这样理解。

因此,我们就需要判断当前要操作的的A[i]的奇偶和queries[i][0]的奇偶,可以分为四种情况:

第一种情况:queries[i][0]为偶数、A[i]也为偶数,即前一次的求和中有A[i],只需加上queries[i][0]的值即可,即sum = sum + queries[i][0]。

第二种情况:queries[i][0]为偶数、A[i]为奇数,即前一次的求和中没有A[i],并且A[i]加上queries[i][0]后也是奇数,所以不用更新sum的值。

第三种情况:queries[i][0]为奇数、A[i]为偶数,即前一次的求和中有A[i],现在A[i]加上queries[i][0]后变成了奇数,需要将前一次求和中的A[i]减掉,即sum = sum - A[i]。

第四种情况:queries[i][0]为奇数、A[i]也为奇数,即前一次的求和中没有A[i],但是A[i]加上queries[i][0]后变成了偶数,需要把这个新的偶数加到sum上,即sum = sum + queries[i][0] + A[i]。

在计算完sum的值后,将sum赋值给新数组对应位置元素,将ueries[i][0] + A[i]的值赋值给A[i],最后返回结果数组。

此解法的时间复杂度为O(M),M为queries的长度,空间复杂度为O(M),M为queries的长度。

public int[] sumEvenAfterQueries2(int[] A, int[][] queries) {
    int sum = 0, i = 0;
    for (int num : A) {
        if (num%2 == 0) {
            sum += num;
        }
    }
    int[] result = new int[queries.length];
    for (int[] arr : queries) {
        int curval = arr[0];
        int preval = A[arr[1]];
        // 做奇偶判断
        if (curval%2 == 0) {
            if (preval%2 == 0) {
                sum = sum + curval;
            } 
        } else {
            if (preval%2 == 0) {
                sum = sum - preval;
            } else {
                sum = sum + curval + preval;
            }
        }
        A[arr[1]] += curval;
        result[i++] = sum;
    }
    return result;
}

04小结

算法专题目前已连续日更超过七个月,算法题文章238+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

OpenClaw使用ClawHub安装skill网络不稳定?Rate limit exceeded报错?试试这个国内高速镜像工具! 使用 ClawHub 安装 Skill 时遇到 🌐 网络不稳、❌ Rate limit 或命令不可用?使用这个镜像工具,轻松解决skill安装问题,⚡ 阅读详情

相关推荐

AFSim 2.9实战指南:从入门到精通的中文教程

本文是AFSim 2.9的实战指南,提供从零开始的中文教程。内容涵盖仿真环境搭建、实体行为控制、脚本编写,到传感器、武器系统集成与复杂想定构建,最后讲解数据记录与结果分析方法,帮助用户快速掌握这一强大的军事仿真框架。

weixin_29281941的博客 949

【LeetCode】985. Sum of Even Numbers After Queries 解题报告(C++)

作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录题目描述题目大意解题方法暴力找规律日期 题目地址:https://leetcode.com/problems/sum-of-even-numbers-after-queries/ 题目描述 We have an array A of integers, and an array q...

负雪明烛 1275

ZYNQ出现问题及解决办法汇总(一直更新)

问题目录使用软件及开发板版本一,vitis在build project时卡住。二,Could not find FPGA device on the board for connection 'Local'三,FPGA configuration failed:DONE PIN is not high四,Could not find ARM device on the board for connection 'Local' 使用软件及开发板版本 开发板:MZ7XA-7020 软件:vitis2020.2(

qq_45057368的博客 1万+

985. Sum of Even Numbers After Queries*

985. Sum of Even Numbers After Queries* https://leetcode.com/problems/sum-of-even-numbers-after-queries/ 题目描述 We have an array A of integers, and an array queries of queries. For the i-th query val = ...

珍妮的选择的博客 287

从Google到LLVM:5种主流clang-format预设风格横向对比(含代码对比图)

本文横向对比了LLVM、Google、Chromium、Mozilla和WebKit五种主流的clang-format预设代码风格。通过同一段C++代码的格式化效果对比图,直观展示了它们在指针对齐、大括号换行、缩进等关键细节上的差异,并提供了基于团队背景与项目基因的选型决策指南。

weixin_28828243的博客 445

Xilnx大学计划/XUP ZYNQ ZU开发板

TUL PYNQ™-ZU板,基于Xilinx Zynq UltraScale+ MPSoC,是为Xilinx大学项目设计的,以支持Xilinx PYNQ (Python Productivi-ty for Zynq)开源框架和嵌入式系统开发。

weixin_57025922的博客 949

LeetCode Sum of Even Numbers After Queries

13131 转载于:https://www.cnblogs.com/ZHONGZHENHUA/p/10876798.html

weixin_30616969的博客 124

【异常】OpenClaw LLM Request Timeout 超时问题的快速定位与落地解决

摘要:OpenClaw系统在执行任务时频繁出现"LLM request timed out"异常。通过分析发现主模型自动调度导致响应延迟,且缺乏超时配置兜底。解决方案包括:1)将主模型切换为低延迟的豆包2.0 Lite;2)保留豆包2.0 Pro和Kimi作为备用;3)重启Gateway服务验证。优化后系统响应速度显著提升,未再出现超时异常。后续建议增加显式超时配置、优化任务拆分和Prompt指令,并建立网络监控机制。(148字)

奶爸的编程之路,也就一周冷个三天 1159

LeetCode-Sum of Even Numbers After Queries

LeetCode Java Sum of Even Numbers After Queries

坟头草一米七五 189

lammps构建高熵合金模型+结构优化初步筛选能量最小的结构

lammps构建高熵合金模型+结构优化初步筛选能量最小的结构

qq_45834835的博客 2605

​非线性分析-增加位移量可能出现下面错误。虽然求解无法在所有时间点完成,但在某些点上仍能够求解部分结果。请参阅“帮助系统”中的故障排除,以了解更多信息详情。由于受约束,规划求解引擎无法收敛

基于收敛状态拆分载荷步‌:在已收敛的载荷步基础上,将未收敛的关键区域拆分为更细的子步,比如在载荷突变点前设置5~10个子步,逐步加载。替换冲突约束为弱约束‌:若存在约束冲突,将刚性约束替换为弱弹簧约束(刚度设为1e-6~1e-4),既限制刚体位移,又不影响结构的变形自由度,命令流:SPRING, NODE, K, 1e-6。部分区域约束完整,但存在未被约束的刚体自由度,或约束类型与大变形/接触逻辑冲突,比如某部件的位移约束在初始阶段有效,但大变形后约束位置脱离实际受力点,导致求解器在该阶段无法收敛。

qq_15963745的博客 149

[985] 令牌放置

import java.util.List; /* * @lc app=leetcode.cn id=985 lang=java * * [985] 令牌放置 * * https://leetcode-cn.com/problems/sum-of-even-numbers-after-queries/description/ * * algorithms ...

weixin_30625691的博客 97

AutoSAR MemMap模块概述及配置参数详解

这个模块是干啥的?它是用来配置Memory Mapping and Compiler Abstraction module。

4509

Leetcode 985.查询后的偶数和(Sum of Even Numbers After Queries)

Leetcode 985.查询后的偶数和 1 题目描述(Leetcode题目链接)   给出一个整数数组 A 和一个查询数组 queries。 对于第 i 次查询,有 val = queries[i][0], index = queries[i][1],我们会把 val 加到 A[index] 上。然后,第 i 次查询的答案是 A 中偶数值的和。 (此处给定的 index = queries[i][1] 是从 0 开始的索引,每次查询都会永久修改数组 A。) 返回所有查询的答案。你的答案应当以数组 answ

qq_39378221的博客 227

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

Leetcode 985. Sum of Even Numbers After Queries

We have an array A of integers, and an array queries of queries. For the i-th query val = queries[i][0], index = queries[i][1], we add val to A[index].  Then, the answer to the i-th query is the sum...

Dive Deep into The Knowledge 514

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

leetcode记录:难度简单:985. Sum of Even Numbers After Queries(计算数组与2维数组查询后的值)

题目出处:https://leetcode.com/problems/sum-of-even-numbers-after-queries/ 题目描述: 我们有一个数组一个整数数组,一个查询的数组查询。对于第i个查询val =查询[i][0], index =查询[i][1],我们将val添加到[index]中。然后,第i个查询的答案是a的偶数的和(这里,给定的index = queries[i...

unAlpaca的博客 255

【Leetcode】985. 查询后的偶数和(Sum of Even Numbers After Queries)

Leetcode - 985 Sum of Even Numbers After Queries (Easy) 题目描述:给出一个整数数组 A 和一个查询数组 queries。对于第 i 次查询,有 val = queries[i][0], index = queries[i][1],我们会把 val 加到 A[index] 上。然后,第 i 次查询的答案是 A 中偶数值的和。 (此处给定的 in...

厉兵秣码 179

leetcode 985. Sum of Even Numbers After Queries(python)

描述 We have an array A of integers, and an array queries of queries. For the i-th query val = queries[i][0], index = queries[i][1], we add val to A[index]. Then, the answer to the i-th query is the sum of the even values of A. (Here, the given index = quer

王大呀呀的博客 298

LeetCode //C - 985. Sum of Even Numbers After Queries

Problem Summary: Given an array nums and a list of queries, each query updates nums by adding a value to a specific index. After each update, compute the sum of all even numbers in the modified array. Solution Approach: Initial Setup: Calculate the initial

Made in Code 415

[Swift]LeetCode985. 查询后的偶数和 | Sum of Even Numbers After Queries

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/streng...

weixin_30627381的博客 122

【Leetcode_总结】985. 查询后的偶数和 - python

Q: 给出一个整数数组 A 和一个查询数组 queries。 对于第 i 次查询,有 val = queries[i][0], index = queries[i][1],我们会把 val 加到 A[index] 上。然后,第 i 次查询的答案是 A 中偶数值的和。 (此处给定的 index = queries[i][1] 是从 0 开始的索引,每次查询都会永久修改数组 A。) 返回所有查...

maka_uir的博客 272

LeetCode C++ 985. Sum of Even Numbers After Queries【Array】简单

We have an array A of integers, and an array queries of queries. For the i-th query val = queries[i][0], index = queries[i][1], we add val to A[index]. Then, the answer to the i-th query is the sum of the even values of A. (Here, the given index = queries

memcpy0的博客 331

[leetcode] 985. Sum of Even Numbers After Queries

Description We have an array A of integers, and an array queries of queries. For the i-th query val = queries[i][0], index = queries[i][1], we add val to A[index]. Then, the answer to the i-th query is the sum of the even values of A. (Here, the given ind

Learning from the mistakes 168

LeetCode 985 Sum of Even Numbers After Queries 解题报告

题目要求 We have an arrayAof integers, and an arrayqueriesof queries. For thei-thqueryval =queries[i][0], index= queries[i][1], we addvaltoA[index]. Then, the answer to thei-th query is th...

weixin_30835649的博客 153

leetcode 985. Sum of Even Numbers After Queries

class Solution { public: vector<int> sumEvenAfterQueries(vector<int>& A, vector<vector<int>>& queries) { int l1 = A.size(); int l2 = queries.size()...

清风吹斜阳 164

【leetcode】985. Sum of Even Numbers After Queries

题目如下: We have an arrayAof integers, and an arrayqueriesof queries. For thei-thqueryval =queries[i][0], index= queries[i][1], we addvaltoA[index]. Then, the answer to thei-th query is...

weixin_34409357的博客 114

#Leetcode# 985. Sum of Even Numbers After Queries

https://leetcode.com/problems/sum-of-even-numbers-after-queries/ We have an arrayAof integers, and an arrayqueriesof queries. For thei-thqueryval =queries[i][0], index= queries[i][1], we...

weixin_30785593的博客 120
上一篇: LeetCode.977-排序数组的平方(Squares of a Sorted Array)
下一篇: LeetCode.989-数组形式的整数做加法(Add to Array-Form of Integer)
程序员小川
博客等级 码龄10年 47粉丝 305原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值