[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-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[i][1] is a 0-based index, and each query permanently modifies the array A.)
Return the answer to all queries. Your answer array should have the answer[i] as the answer to the i-th query.
Example 1:
Input: A = [1, 2, 3, 4], queries = [[1, 0], [-3, 1], [-4, 0], [2, 3]]
Output: [8, 6, 2, 4]
Explanation:
At the beginning, the array is [1, 2, 3, 4]
After adding 1 to A[0], the array is [2, 2, 3, 4], and the sum of even values is 2 + 2 + 4 = 8.
After adding -3 to A[1], the array is [2, -1, 3, 4], and the sum of even values is 2 + 4 = 6.
After adding -4 to A[0], the array is [-2, -1, 3, 4], and the sum of even values is -2 + 4 = 2.
After adding 2 to A[3], the array is [-2, -1, 3, 6], and the sum of even values is -2 + 6 = 4.
Note:
1 <= A.length <= 10000-10000 <= A[i] <= 100001 <= queries.length <= 10000-10000 <= queries[i][0] <= 100000 <= queries[i][1] < A.length
Related Topics
Array
Solution
求出原数组中的偶数和
对于每一个查询,对于数组元素
A[queries[i][1]]的影响,按以下四种情况处理:- 之前为奇数,之后为奇数:无需操作;
- 之前为奇数,之后为偶数:在原偶数和的基础上加上这个新增的偶数;
- 之前为偶数,之后为奇数:在原偶数和的基础上去掉这个之前的偶数;
- 之前为偶数,之后为奇数:在原偶数和的基础上加上一个变化量(可能为正,也可能为负);
将每次查询得到的偶数和放入结果数组中即为所求。
public class Solution
{
public int[] SumEvenAfterQueries(int[] A, int[][] queries)
{
int[] ret = new int[queries.GetLength(0)];
int sum = (from x in A where x % 2 == 0 select x).Sum();
for(int i = 0; i < queries.GetLength(0); i++)
{
int before = A[queries[i][1]];
A[queries[i][1]] += queries[i][0];
if(before % 2 == 0)
{
if(A[queries[i][1]] % 2 == 0)
{
int delta = A[queries[i][1]] - before;
sum += delta;
}
else
{
sum -= before;
}
}
else
{
if(A[queries[i][1]] % 2 == 0)
{
sum += A[queries[i][1]];
}
else
{
// no operation
}
}
ret[i] = sum;
}
return ret;
}
}
[Solution] 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 ...
- 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 = queri ...
- #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 ...
- 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 ...
- [Swift]LeetCode985. 查询后的偶数和 | 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] ...
随机推荐
- Restful OData Protocol
Web服务有两种实现方式: 一是SOAP协议方式 二是REST方式. SOAP是一套完整的实现Web服务的解决方案.这里有必要先简单了解SOAP方式的Web服务,然后对比SOAP方式,我们会发现RES ...
- vmware 中配置centos 7 静态IP
虚拟机配置成静态IP可以保证每次宿主机器重启后,虚拟机的IP保持不变,这对于学习集群环境下的软件(如hadoop集群,mysql等数据库集群)很有用. vmware workstation 中装好li ...
- Selenium之动作链(ActionChains)
用selenium做自动化,有时候会遇到需要模拟鼠标操作才能进行的情况,比如单击.双击.点击鼠标右键.拖拽等等.而selenium给我们提供了一个类来处理这类事件——ActionChains se ...
- Unity Graphics(一):选择一个光照系统
原文链接 Choosing a Lighting Technique https://unity3d.com/learn/tutorials/topics/graphics/choosing-ligh ...
- 封装poi导出篇
前言 先写的导入,以为导出会很简单,没想到导出的东西也不少,基于常用的几种样式和校验写了一个简单的导出,包括时间,数字,文字长度,下拉框,提示框校验,基础样式包括字体,字体颜色,背景颜色等功能,可以使 ...
- 正确的学python方式
首先呢,和其他的各种学习都一样,你一定要明白你学习的目标是什么.有的人想要通过学习Python,转行成程序员,实现行业上的转变:有的人希望通过学习Python,在现有的岗位上提升自己:当然也有很多人只 ...
- HTTPS 基本流程3
前文说到了6 组key material, 12个hash 值,非常迷惑, 今天才搞明白, 原来所有这些内容就是 对称密钥的内容. 上面的图 虽然不是很清晰,但是, 其实也已经写明白了, 就是 右边的 ...
- [UNITY 5.4 UGUI] 控件重叠触摸穿透
问题. imge 和 button重叠时,imge 覆盖在button上面,导致点击事件无法传递到button. 1.给imge 添加 [Canvas Group]组件 2.修改[Canvas Gro ...
- Ping++支付
第一次接触支付啊,有点小激动,所以写下这篇随笔以防以后忘记. ping++的文档还有服务都是挺好的,当你注册之后,就会给你发邮件.截图如下: 是不是感觉服务很不错. 接下来直入正题. 首先,我们需要加 ...
- vue.js响应式原理解析与实现
vue.js响应式原理解析与实现 从很久之前就已经接触过了angularjs了,当时就已经了解到,angularjs是通过脏检查来实现数据监测以及页面更新渲染.之后,再接触了vue.js,当时也一度很 ...