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 = 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 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. 1 <= A.length <= 10000
  2. -10000 <= A[i] <= 10000
  3. 1 <= queries.length <= 10000
  4. -10000 <= queries[i][0] <= 10000
  5. 0 <= queries[i][1] < A.length

代码:

class Solution {
public:
vector<int> sumEvenAfterQueries(vector<int>& A, vector<vector<int>>& queries) {
int na = A.size(), nq = queries.size();
int sum = 0;
vector<int> ans;
for(int i = 0; i < na; i ++) {
if(A[i] % 2 == 0) sum += A[i];
} for(int i = 0; i < nq; i ++) {
int pos = queries[i][1], val = queries[i][0];
if(A[pos] % 2 == 0) {
if((A[pos] + val) % 2 == 0)
sum += val;
else sum -= A[pos];
} else {
if((A[pos] + val) % 2 == 0)
sum += val + A[pos];
}
A[pos] += val;
ans.push_back(sum);
}
return ans;
}
};

  

#Leetcode# 985. Sum of Even Numbers After Queries的更多相关文章

  1. 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 ...

  2. 【LEETCODE】47、985. Sum of Even Numbers After Queries

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  3. 【Leetcode_easy】985. Sum of Even Numbers After Queries

    problem 985. Sum of Even Numbers After Queries class Solution { public: vector<int> sumEvenAft ...

  4. [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 ...

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

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 找规律 日期 题目地址:https://lee ...

  6. 【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 ...

  7. 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] ...

  8. 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] ...

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

    这是悦乐书的第370次更新,第398篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第232题(顺位题号是985).有一个整数数组A和一个查询数组queries. 对于第i ...

随机推荐

  1. Eclipse修改Maven仓库配置

    修改Apache-Maven\conf下的settings.xml文件 <?xml version="1.0" encoding="UTF-8"?> ...

  2. Python 提案

    学习Java 不可不知JSR,学习Python自然也得知道 PEP了 1- PEP简介 PEP是Python增强提案(Python Enhancement Proposal)的缩写.https://w ...

  3. LeetCode算法题-Reverse Vowels of a String(Java实现-四种解法)

    这是悦乐书的第206次更新,第218篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第74题(顺位题号是345).编写一个函数,它将一个字符串作为输入,并仅反转一个字符串的 ...

  4. Docker: docker container常用命令实战

    容器管理,容器常用选项 选项 描述 -i, –interactive 交互式 -t, –tty 分配一个伪终端 -d, –detach 运行容器到后台 -e, –env 设置环境变量 -p, –pub ...

  5. CSS鼠标悬浮DIV后显示DIV外的按钮

    昨天写样式遇到个问题,如何让鼠标悬浮DIV后,显示DIV外的按钮,可以点击到按钮. 效果如下: 问题: 在DIV hover时候将按钮设为display: block,这是很直接的想法,但是这有个问题 ...

  6. Shell and DOS

    long long ago 自己便想总结下shell命令以及dos常用的命令,毕竟实际实践中会经常用到,用的好的批处理或者shell脚本会事半功倍,好了,废话不多说,开始. shell echo [字 ...

  7. win21api、win32gui、win32con三个模块操作系统窗口时一些小技巧

    下面这段脚本是操作一个浏览器上弹窗,打开文件窗口,由于脚本 执行速度快,当时未添加第2行的延时时,脚本顺利的执行成功,但弹的窗却没有进行操作,建议后续如果脚本执行到打开弹窗时,延时个几秒再去操作所弹窗 ...

  8. C#进阶のMEF注入

    1.什么是MEF 先来看msdn上面的解释:MEF(Managed Extensibility Framework)是一个用于创建可扩展的轻型应用程序的库. 应用程序开发人员可利用该库发现并使用扩展, ...

  9. Linux三剑客-AWK

    1.什么是awk AWK是一种处理文本文件的语言,是一个强大的文本分析工具.有统计和计算功能. 之所以叫AWK是因为其取了三位创始人 Alfred Aho,Peter Weinberger, 和 Br ...

  10. re库

    一.Re库的主要功能: 函数 功能 re.search() 在一个字符串中搜索匹配正则表达式的第一个位置,返回match对象 re.match() 在一个字符串的开始位置匹配正则表达式,返回match ...