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. Linux运维小知识

    自己日常用到的命令稍微备份一下: 版本确认 CentOS / RedHat Enterprise cat /etc/redhat-release Ubuntu cat /etc/lsb-release ...

  2. goflyway简单使用

    前言 一个朋友最近新买的搬瓦工ip突然被强了,要等10周左右才能免费更换ip.而恰巧在网上看到了Goflyway 进阶教程:KCP 协议复活被墙IP 决定试一试.在vultr上临时搭建了测试环境,可能 ...

  3. python模拟大数据登陆

    #针对tableu 撰写的大数据框架 #tesseract 识别简单的验证码 不多说  直接上代码 # coding:utf-8 from selenium import webdriver from ...

  4. 7.2.5 多层嵌套的if语句

    7.2.5 多层嵌套的if语句 在编写程序的代码之前要先规划好.首先,要总体设计一下程序. 为方便起见,程序应该使用一个连续的循环让用户能连续输入待测试的 数.这样,测试一个新的数字不必每次都要重新运 ...

  5. IDEA注册码分享

    IntelliJ IDEA IDEA 2018 激活注册码分享鼠标连续 三下左键点击 选中,再Ctrl+C 即可复制. CSDN在末尾会带上博客的说明,请删除后,复制到 IDEA中激活. 注册码激活: ...

  6. 神经网络常用的Numpy功能笔记

    数组初始化 x=np.array([[1,2]]) x=np.zeros((2,3)) 生成随机数 w=np.random.randn(2,3) PIL image转换成array img = np. ...

  7. [STM32F103]外部中断

    ① 初始化IO口为输入. GPIO_Init(); ② 开启IO口复用时钟. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); ③ 设置IO口与中 ...

  8. int x ; x+1<x;公式成立

    直接上代码: Console.WriteLine("int取值范围 -2147483648-2147483647");int x = 2147483647;// Console.W ...

  9. jquery中文档处理的总结

    jQuery文档处理总结 1.返回值:append(content|fn) $("p").append("<b>Hello</b>"); ...

  10. [UnityShader效果]01.Mask

    参考链接: https://blog.csdn.net/akof1314/article/details/50428200 1.Mask.shader // Unity built-in shader ...