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

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

Related Topics

Array

Solution

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

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

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

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

public class Solution
{
public int[] SumEvenAfterQueries(int[] A, int[][] queries)
{
int[] ret = new int[queries.GetLength(0)];
int sum = (from x in A where x % 2 == 0 select x).Sum(); for(int i = 0; i < queries.GetLength(0); i++)
{
int before = A[queries[i][1]];
A[queries[i][1]] += queries[i][0];
if(before % 2 == 0)
{
if(A[queries[i][1]] % 2 == 0)
{
int delta = A[queries[i][1]] - before;
sum += delta;
}
else
{
sum -= before;
}
}
else
{
if(A[queries[i][1]] % 2 == 0)
{
sum += A[queries[i][1]];
}
else
{
// no operation
}
}
ret[i] = sum;
} return ret;
}
}

[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. ajax(读取json数据)

    ajax知识点: 语法:$.ajax(路由,请求方式,返回的数据类型,数据参数,回调函数) url: "路由", type:"",默认get请求(get或pos ...

  2. 你云我云•兄弟夜谈会 第三季 企业IT架构

    你云我云•兄弟夜谈会 第三季 企业IT架构 你云我云•兄弟夜谈会 第二季 5G 你云我云•兄弟夜谈会 第一季 企业云 0. 概况 时间:2019年2月23日 22:00~23:30 主题:企业IT架构 ...

  3. [转][C#]服务安装卸载命令

    c:\windows\microsoft.net\Framework\v2.0.50727\installutil.exe  [/u] X:\服务.exe

  4. java核心-多线程-线程类-Callable、Future和FutureTask

    基本概念 <1>Callable,Callable和Runnable差不多,两者都是为那些其实例可能被另一个线程执行的类而设计的,最主要的差别在于Runnable不会 返回线程运算结果,C ...

  5. [UE4]瞬移前后屏幕亮度变化,Get Player Camera Manager.Start Camera Fade

    From Alpha:开始的颜色透明度 To Alpha:结束的颜色透明度 Duration:过渡所使用的时间(单位:秒) Color:屏幕变化的颜色 Hold when finished:过渡时间结 ...

  6. oracle导出表的建表语句拼接SQL

    前段时间有个需求需要导出数据库的500张表结构,使用PLSQLDEV工具也可以导出建表语句,但是需要手动一个表一个表选,非常费劲.就写了个拼接sql. select 'select dbms_meta ...

  7. python,列表,元祖,字典

    list 列表 li = [1,",[3,4]] 1.用中括号括起来 2.用,来分割每一个元素 3.列表中的元素可以是,数字,字符串,列表,布尔值 4.“集合”,内部可以放置任何东西 li ...

  8. Linux系统中的计划任务

    在系统的工作管理中,我们经常会有需要去告诉电脑某些特定的时间执行一些操作,比如定时提醒工作人员需要做什么事情,或者在每天凌晨进行文件备份等等.这就需要某些命令来达成计划任务. 计划任务可以大体上分成两 ...

  9. 1.1python解决数学建模之席位分配问题

    一:上代码 #比例法def rate_method(p,n):    lst =[] #保存各组席位数    sum_ =sum(p)    #人数和    k =0#临时变量    for i in ...

  10. this 指向详细解析(箭头函数)

    前言 this 指向问题是入坑前端必须了解知识点,现在迎来了ES6时代,因为箭头函数的出现,所以感觉有必要对 this 问题梳理一下,遂有此文 在非箭头函数下, this 指向调用其所在函数的对象,而 ...