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数组,共四种情况:加运算之前是偶数,之后是奇数:总和-旧值;偶数-》偶数:总和+新值;奇数-》偶数:总和+旧值+新值;奇数-》奇数:总和不变。

class Solution {
    public int[] sumEvenAfterQueries(int[] A, int[][] queries) {
        int[] result = new int[queries.length];
        int evenSum = 0;
        for (int i1 : A) {
            if (i1 % 2 == 0) {
                evenSum += i1;
            }
        }
        for (int i = 0; i < queries.length; i++) {
            int index = queries[i][1];
            int val = queries[i][0];
            int old = A[index];

            A[index] = A[index] + val;
            if (old % 2 == 0 && A[index] % 2 != 0) {
                result[i] = evenSum - old;
                evenSum = result[i];
                continue;
            }
            if (old % 2 == 0 && A[index] % 2 == 0) {
                result[i] = evenSum + val;
                evenSum = result[i];
                continue;
            }
            if (old % 2 != 0 && A[index] % 2 == 0) {
                result[i] = evenSum + old + val;
                evenSum = result[i];
                continue;
            }
            if (old % 2 != 0 && A[index] % 2 != 0) {
                result[i] = evenSum;
                evenSum = result[i];
            }
        }
        return result;
    }
}

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. xirr函数

    内部收益计算函数 曾经看过一个帖子:有一个理财产品,每年年初存入10000元,每年年底得到利息1000元.持续5年,5年后返还本金50000元:问:利率是多少?下面有个回复:每年存10000,利息10 ...

  2. ActionFilterAttribute 全局记录API日志

    1.API项目下创建MonitorApiAttribute public class MonitorApiAttribute : ActionFilterAttribute { private sta ...

  3. android 中activity重启的方法

    private void reLoadActivity(){ Intent intent = new Intent(context, SettingsActivity.class); intent.s ...

  4. Linux配置虚拟内存

    我的Linux内存很少,所以我给它弄个虚拟内存 首先建立一个1G的空文件: dd if=/dev/zero of=/home/swapfile bs=64M count=16 格式化为swap: mk ...

  5. C语言的split功能

    其它高级语言都有字符串的split功能,但C没有系统自带的,只能自己写一个了. void c_split(char *src, const char *separator, int maxlen, c ...

  6. {"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}

    ElasticSearch-head 查询报 406错误码 {"error":"Content-Type header [application/x-www-form-u ...

  7. js 解密

    需求:爬取https://www.xuexi.cn/f997e76a890b0e5a053c57b19f468436/018d244441062d8916dd472a4c6a0a0b.html页面中的 ...

  8. 通过ldap验证svn服务

    1.简单介绍: 这里需要介绍一点的就是svn服务器的验证是需要通过SASL机制的,那么SASL全称为(Simple Authentication and security Layer),是一种用来扩充 ...

  9. java.lang.OutOfMemoryError: PermGen space解决方法

  10. hive 学习

    多表查询 SELECT a.user_uid_type, a.user_uid, c.user_id, c.user_type FROM mytable1 a JOIN mytable2 b ON(a ...