[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] ...
随机推荐
- [蓝桥杯]PREV-27.历届试题_蚂蚁感冒
问题描述 长100厘米的细长直杆子上有n只蚂蚁.它们的头有的朝左,有的朝右. 每只蚂蚁都只能沿着杆子向前爬,速度是1厘米/秒. 当两只蚂蚁碰面时,它们会同时掉头往相反的方向爬行. 这些蚂蚁中,有1只蚂 ...
- python中Multiprocessing
import multiprocessing as mp #该函数不能有返回值,如果需要则应该将值放在queue中 def job(a,b): print('aaaa') if __name__ == ...
- 如何限制指定textFiled第三方输入法切换
在有些项目中需要用到输入纯数字的键盘,并且还不能切换到第三方输入法! textFiled.secureTextEntry = YES; [textFiled addTarget:self action ...
- 关于Unity单个对象多个脚本的Update调用的时序问题
先说几句废话, 最近在研究Unity, 这玩意用起来比较简单, 而且商店里还有各种插件, 初学者也能轻松拼凑出一个像模像样的游戏(顺便说一句,自己做着玩就无所谓了,但随便拼凑个辣鸡丢出来骗钱就不好了) ...
- dubbo 调用服务超时
先贴出错误报告: Failed to invoke the method *** in the service ***. Tried times of the providers [] (/) on ...
- Flutter windows环境安装 + 模拟设备 + 项目运行
目录: 一.JDK安装 1.1.JDK下载 1.2.环境变量配置 1.3.测试 二.ANDROID-SDK安装 2.1.下载 2.2.环境变量配置 三.Flutter安装 3.1.下载 3.2.环境变 ...
- 秘制牛肉Alpha阶段项目展示
秘制牛肉Alpha阶段项目展示 1.团队成员和个人博客 · 左顺:"我是左顺,秘制牛肉队开发人员". · 王尖兵:"C,java,html5都会一点的菜鸡,没做过团队项目 ...
- How does the compilation and linking process work?
The compilation of a C++ program involves three steps: Preprocessing: the preprocessor takes a C++ s ...
- 将自己的SpringBoot应用打包发布到Linux下Docker中
目录 将自己的SpringBoot应用打包发布到Linux下Docker中 1. 环境介绍 2. 开始前的准备 2.1 开启docker远程连接 2.2 新建SpringBoot项目 3. 开始构建我 ...
- LeetCode 24. Swap Nodes in Pairs 成对交换节点 C++/Java
Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...