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.

有两个数组A和queries,queries是个二维数组,第一个元素是值,第二个元素是索引,循环queries数组,把queries的每个值加到A数组对应索引位置上。求出每一次循环后A数组中的偶数和。

思路:先求出A数组中的偶数和。再循环queries数组,共四种情况:加运算之前是偶数,之后是奇数:总和-旧值;偶数-》偶数:总和+新值;奇数-》偶数:总和+旧值+新值;奇数-》奇数:总和不变。

  1. class Solution {
  2. public int[] sumEvenAfterQueries(int[] A, int[][] queries) {
  3. int[] result = new int[queries.length];
  4. int evenSum = 0;
  5. for (int i1 : A) {
  6. if (i1 % 2 == 0) {
  7. evenSum += i1;
  8. }
  9. }
  10. for (int i = 0; i < queries.length; i++) {
  11. int index = queries[i][1];
  12. int val = queries[i][0];
  13. int old = A[index];
  14. A[index] = A[index] + val;
  15. if (old % 2 == 0 && A[index] % 2 != 0) {
  16. result[i] = evenSum - old;
  17. evenSum = result[i];
  18. continue;
  19. }
  20. if (old % 2 == 0 && A[index] % 2 == 0) {
  21. result[i] = evenSum + val;
  22. evenSum = result[i];
  23. continue;
  24. }
  25. if (old % 2 != 0 && A[index] % 2 == 0) {
  26. result[i] = evenSum + old + val;
  27. evenSum = result[i];
  28. continue;
  29. }
  30. if (old % 2 != 0 && A[index] % 2 != 0) {
  31. result[i] = evenSum;
  32. evenSum = result[i];
  33. }
  34. }
  35. return result;
  36. }
  37. }

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

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

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

  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. LeetCode.985-查询后偶数的总和(Sum of Even Numbers After Queries)

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

随机推荐

  1. Unix即IDE

    前言 在图形界面下大家都想要这种能够集成在一起的工具,那是因为这类窗口应用除了用复制粘贴,没有别的方法使他们更好地协同工作,它们缺失一种 共用接口(common interface) . 有关这个问题 ...

  2. 分布式-JOB(XXL-Job)

    为什么使用xxl-job,不使用qz: 缺少补偿机制 不支持集群 不支持路由策略 统计任务执行 平台管理 监控,报警邮箱 幂等性:一次请求和多次请求得到相同的结果,不会因为多次的请求,导致最后的数据不 ...

  3. rocketMQ(二 )Centos7 集群

    rocketMQ集群: 在运用中流程一般 是在程序中使用代码编辑生产者,将所需要的消息发送到rocketmq中,然后另一个程序编辑消费者从rocketmq里面获取消息.rocketmq集群 需要对na ...

  4. c# 简单方便的连接oracle方式

    通过nuget安装ManagedDataAccess (自动生成的config里面的配置都可以删掉) winform程序,拖出一个datagridview和button using Oracle.Ma ...

  5. [UE4]瞬移之后的朝向

    一.Set Actor Rotation:设置绝对朝向:Set Actor Relative Rotation:设置相对朝向 二.瞬移以后,角色的朝向和相机的朝向是不一样的,和头显的朝向不是同一个朝向 ...

  6. 爬虫系列4:Requests+Xpath 爬取动态数据

    爬虫系列4:Requests+Xpath 爬取动态数据 [抓取]:参考前文 爬虫系列1:https://www.cnblogs.com/yizhiamumu/p/9451093.html [分页]:参 ...

  7. Sysbench 1.0.17安装与测试

    Sysbench安装与测试 1.安装: cd /usr/local/src wget https://codeload.github.com/akopytov/sysbench/tar.gz/1.0. ...

  8. Hadoop 2.8集群安装及配置记录

    第一部分:环境配置(含操作系统.防火墙.SSH.JAVA安装等) Hadoop 2.8集群安装模拟环境为: 主机:Hostname:Hadoop-host,IP:10.10.11.225 节点1:Ho ...

  9. Skyline TerraExplorer 7.0- 扩展信息树

    Skyline TerraExplorer V7增加了一个扩展信息树的控件TEInformationWindowEx.  该控件能够将TE3DWindowEx窗口里面的对象显示为信息树的方式.TEIn ...

  10. Linq to SQL -- Group By、Having和Exists、In、Any、All、Contains

    Group By/Having操作符 适用场景:分组数据,为我们查找数据缩小范围. 说明:分配并返回对传入参数进行分组操作后的可枚举对象.分组:延迟 1.简单形式: var q = from p in ...