• 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:

  1. Input: A = [1, 2, 3, 4], queries = [[1, 0], [-3, 1], [-4, 0], [2, 3]]
  2. Output: [8, 6, 2, 4]
  3. Explanation:
  4. At the beginning, the array is [1, 2, 3, 4]
  5. After adding 1 to A[0], the array is [2, 2, 3, 4], and the sum of even values is 2 + 2 + 4 = 8.
  6. After adding -3 to A[1], the array is [2, -1, 3, 4], and the sum of even values is 2 + 4 = 6.
  7. After adding -4 to A[0], the array is [-2, -1, 3, 4], and the sum of even values is -2 + 4 = 2.
  8. 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

Related Topics

Array

Solution

  1. 求出原数组中的偶数和

  2. 对于每一个查询,对于数组元素 A[queries[i][1]] 的影响,按以下四种情况处理:

    • 之前为奇数,之后为奇数:无需操作;
    • 之前为奇数,之后为偶数:在原偶数和的基础上加上这个新增的偶数;
    • 之前为偶数,之后为奇数:在原偶数和的基础上去掉这个之前的偶数;
    • 之前为偶数,之后为奇数:在原偶数和的基础上加上一个变化量(可能为正,也可能为负);

    将每次查询得到的偶数和放入结果数组中即为所求。

  1. public class Solution
  2. {
  3. public int[] SumEvenAfterQueries(int[] A, int[][] queries)
  4. {
  5. int[] ret = new int[queries.GetLength(0)];
  6. int sum = (from x in A where x % 2 == 0 select x).Sum();
  7. for(int i = 0; i < queries.GetLength(0); i++)
  8. {
  9. int before = A[queries[i][1]];
  10. A[queries[i][1]] += queries[i][0];
  11. if(before % 2 == 0)
  12. {
  13. if(A[queries[i][1]] % 2 == 0)
  14. {
  15. int delta = A[queries[i][1]] - before;
  16. sum += delta;
  17. }
  18. else
  19. {
  20. sum -= before;
  21. }
  22. }
  23. else
  24. {
  25. if(A[queries[i][1]] % 2 == 0)
  26. {
  27. sum += A[queries[i][1]];
  28. }
  29. else
  30. {
  31. // no operation
  32. }
  33. }
  34. ret[i] = sum;
  35. }
  36. return ret;
  37. }
  38. }

[Solution] 985. Sum of Even Numbers After Queries的更多相关文章

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

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

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

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

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

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

  5. #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 ...

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

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

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

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

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

随机推荐

  1. SpringBoot启动源码探究---getRunListener()

    该方法目的是获取SpringApplicationRunListener getRunListener()-----调用----> getSpringFactoriesInstances()-- ...

  2. 【rabbitmq】RabbitMQ 集群与网络分区

    网络分区(network partitions) 官网-网络分区 网络设备故障导致的网络分裂.比如,存在A\B\C\D\E五个节点,A\B处于同一子网,B\C\D处于另外一子网,中间通过交换机相连.若 ...

  3. 红米手机5 Plus完美刷成开发版获取root权限的教程

    小米的设备不同手机型号通常情况小米官方都提供两个不同系统,可分为稳定版和开发版,稳定版没有提供ROOT超级权限管理,开发版中就提供了ROOT超级权限,较多时候我们需要使用的一些功能强大的工具,都需要在 ...

  4. 酒店web认证802.11x+ROS共享NAT上网

    网络环境: 维也纳酒店-上网方式 web认证上网(wifi有线) 设备-ROS RB952Ui-5AC2ND一台笔记本电脑一台 web认证的三个特征,我们下面ROS的设置就是为了满足下面三个条件来分布 ...

  5. rabbit原理及项目应用

    1.rabbitMQ是什么? mq是由erlang语言开发的开源的amqp的实现. 2.rabbitMQ的基本原理是什么? 使用RabbitMQ,首先需要与rabbitMQ的visiu host建立连 ...

  6. 刘志梅 201771010115 《面向对象程序设计(java)》 第十八周学习总结

    实验十八 总复习 实验时间 2018-12-30 1.实验理论知识 (1)第一个关键词:public称为访问修饰符,用于控制程序的其他部分对代码的访问级别. 第二个关键词:class表明java程序中 ...

  7. JAVA之Mybatis基础入门--框架搭建与简单查询

    JAVA中,操作数据库有JDBC.hibernate.Mybatis等技术,今天整理了下,来讲一讲下Mybatis.也为自己整理下文档: hibernate是一个完全的ORM框架,是完全面向对象的.但 ...

  8. 使用Microsoft自带的小工具将可执行文件(.exe)注册为系统服务

    首先,我们从Microsoft下载Windows Resource Kits,Download 下载完成后,运行rktools.exe进行安装. 安装完成后,我们打开安装目录,将其中的"in ...

  9. Linq to SQL -- Select、Distinct和Count、Sum、Min、Max、Avg

    Select/Distinct操作符 适用场景:o(∩_∩)o… 查询呗. 说明:和SQL命令中的select作用相似但位置不同,查询表达式中的select及所接子句是放在表达式最后并把子句中的变量也 ...

  10. vs2017安装和使用教程(详细)

    借鉴:https://blog.csdn.net/qq_36556893/article/details/79430133#一.官网下载