985. Sum of Even Numbers After Queries
We have an array
Aof integers, and an arrayqueriesof queries.For the
i-th queryval = queries[i][0], index = queries[i][1], we add val toA[index]. Then, the answer to thei-th query is the sum of the even values ofA.(Here, the given index = queries[i][1] is a 0-based index, and each query permanently modifies the array A.)
Return the answer to all queries. Your
answerarray should haveanswer[i]as the answer to thei-th query.有两个数组A和queries,queries是个二维数组,第一个元素是值,第二个元素是索引,循环queries数组,把queries的每个值加到A数组对应索引位置上。求出每一次循环后A数组中的偶数和。
思路:先求出A数组中的偶数和。再循环queries数组,共四种情况:加运算之前是偶数,之后是奇数:总和-旧值;偶数-》偶数:总和+新值;奇数-》偶数:总和+旧值+新值;奇数-》奇数:总和不变。
class Solution {
public int[] sumEvenAfterQueries(int[] A, int[][] queries) {
int[] result = new int[queries.length];
int evenSum = 0;
for (int i1 : A) {
if (i1 % 2 == 0) {
evenSum += i1;
}
}
for (int i = 0; i < queries.length; i++) {
int index = queries[i][1];
int val = queries[i][0];
int old = A[index];
A[index] = A[index] + val;
if (old % 2 == 0 && A[index] % 2 != 0) {
result[i] = evenSum - old;
evenSum = result[i];
continue;
}
if (old % 2 == 0 && A[index] % 2 == 0) {
result[i] = evenSum + val;
evenSum = result[i];
continue;
}
if (old % 2 != 0 && A[index] % 2 == 0) {
result[i] = evenSum + old + val;
evenSum = result[i];
continue;
}
if (old % 2 != 0 && A[index] % 2 != 0) {
result[i] = evenSum;
evenSum = result[i];
}
}
return result;
}
}
985. Sum of Even Numbers After Queries的更多相关文章
- 【LEETCODE】47、985. Sum of Even Numbers After Queries
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【Leetcode_easy】985. Sum of Even Numbers After Queries
problem 985. Sum of Even Numbers After Queries class Solution { public: vector<int> sumEvenAft ...
- [Solution] 985. Sum of Even Numbers After Queries
Difficulty: Easy Question We have an array A of integers, and an array queries of queries. For the i ...
- #Leetcode# 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 ...
- 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 = queri ...
- LC 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] ...
- 【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 = quer ...
- 【LeetCode】985. Sum of Even Numbers After Queries 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 找规律 日期 题目地址:https://lee ...
- LeetCode.985-查询后偶数的总和(Sum of Even Numbers After Queries)
这是悦乐书的第370次更新,第398篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第232题(顺位题号是985).有一个整数数组A和一个查询数组queries. 对于第i ...
随机推荐
- python基础知识9---字符串拼接,深浅拷贝,三元运算
一.字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3 ...
- rabbitMQ 在 windows 64位环境下无法启动(提示乱码)的解决方法
执行start命令时,提示乱码 解决方法: Set the environment variable “RABBITMQ_BASE” to “c:\rabbitmq”, uninstall the s ...
- Linux运维宝典:最常用的150个命令汇总
一.线上查询及帮助命令(2个) 二.文件和目录操作命令(18个) 三.查看文件及内容处理命令(21个) 四.文件压缩及解压缩命令(4个) 五.信息显示命令(11个) 六.搜索文件命令(4个) 七.用户 ...
- Git技能图
- json相关安全问题
前言: 最近经常遇到json的事情,由于不懂所以系统的学习一下,记录此贴. 00x1: JSON是啥?Java Objiect Notaton 谷歌翻译过来就是JS对象标记,是一种轻量级的数据交换格式 ...
- c# datatable按主键合并相同主键返回新的datatable
一.概述: 在python转c#时,python中pandas.merge可以按主键合并两个datatable,苦苦找了很久,希望c#也有同样的函数,未果,就自己写了一个,目前测试没问题,同样我也考虑 ...
- [trouble] error connecting to master 'repl@192.168.1.107:3306' - retry-time: 60 retries: 86400
本文转自https://www.cnblogs.com/hellotracy/articles/5183057.html因为碰到同样的问题所以记录下. 很多时候,就算thread 正在进行,也不代表没 ...
- 数据泵导出报ORA-01555 ORA-22924
最近对某数据库执行数据泵导出操作时碰到如下错误:Processing object type DATABASE_EXPORT/SCHEMA/JOBProcessing object type DATA ...
- MemCache在网站中的使用
MemCache安装好后,网站一直没法使用,后来查找资料,发现需要在配置文件里写几行代码,如下所示 <enyim.com> <memcached protocol="Tex ...
- 研究并尝试改进Vyeshal
我想研究Vyeshal,这样就不可避免的要用到游戏中的语音.然而游戏内的语音文件是.bank类型的,如何转化为可听文件是个问题.