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. June 6. 2018 Week 23rd Wednesday

    You are confined only by the walls you build yourself. 限制你的只有你自己筑起的墙. From Andrew Murphy. Let's repe ...

  2. 正则表达式工具RegexBuddy

    1 下载 RegexBuddy 并安装 安装后的界面如下:     2 切换布局 点击右上角的彩色格子图标,选择 Side by Side Layout:     这种布局的好处是,Create 面板 ...

  3. RabbitMQ用户增删及权限控制

    RabbitMQ用户增删及权限控制 用户角色分类 none:无法登录控制台 不能访问 management plugin,通常就是普通的生产者和消费者. management:普通管理者. 仅可登陆管 ...

  4. linux7 udev的生效

    这篇文章主要介绍在Oracle Linux 7中如何使用udev来设置用户自定义的设备名.在Oracle Linux 7 中的设置方法与之前的Linux版本有较大差别. 下面的例子的对/dev/sdb ...

  5. solaris下安装oracle 11g与11g rac

    1.To Find Swap, RAM, and OS Version 对于 Solaris 操作系统,查看 Swap, RAM, and OS Version 的方法: swap -l /usr/s ...

  6. 普通用户使用Docker

    1.查询是否有docker组 cat /etc/group 如果没有可以通过该命令添加(一般默认是有的) sudo groupadd docker 2.将当前用户添加到docker组 sudo use ...

  7. mysql从5.5直接升级到5.7后,执行mysql_upgrade速度很慢且执行结束后数据目录大小增加一倍及 mysqlpump备份出现1577错误

    mysql官网不支持夸大版本升级,比如跳过5.6直接升级到5.7,但由于一些客观原因,项目需要从5.5直接升级到5.7,以下是具体操作 1.备份之前mysql,(数据量少,可直接拷贝安装目录及data ...

  8. ABAQUS复合材料

    转自)http://meiniyuan.blog.sohu.com/134141022.html 有很多帖子相当的不错,介绍了复合材料研究的基本的原理和方法,发现好的帖子再更新:0 c3 n0 ~+ ...

  9. RabbitMQ详解(三)------RabbitMQ的五种队列

    上一篇博客我们介绍了RabbitMQ消息通信中的一些基本概念,这篇博客我们介绍 RabbitMQ 的五种工作模式,这也是实际使用RabbitMQ需要重点关注的. 这里是RabbitMQ 官网中的相关介 ...

  10. Netty入门(三)之web服务器

    Netty入门(三)之web服务器 阅读前请参考 Netty入门(一)之webSocket聊天室 Netty入门(二)之PC聊天室 有了前两篇的使用基础,学习本文也很简单!只需要在前两文的基础上稍微改 ...