题目如下:

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.

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

解题思路:题目很简单,有一点需要注意,就是每次执行query后,不需要遍历整个数组求出所有偶数的值。只需要记录上一次偶数的和,执行query前,如果该值是偶数,用上一次的和减去该值;执行query后,如果新值是偶数,用上一次的和加上这个新值,就可以得到这次query执行后的偶数总和。

代码如下:

  1. class Solution(object):
  2. def sumEvenAfterQueries(self, A, queries):
  3. """
  4. :type A: List[int]
  5. :type queries: List[List[int]]
  6. :rtype: List[int]
  7. """
  8. res = []
  9. count = None
  10. for val,inx in queries:
  11. before = A[inx]
  12. A[inx] += val
  13. after = A[inx]
  14. if count == None:
  15. count = sum(filter(lambda x: x % 2 == 0,A))
  16. else:
  17. if before % 2 == 0:
  18. count -= before
  19. if after % 2 == 0:
  20. count += after
  21. res.append(count)
  22. return res

【leetcode】985. Sum of Even Numbers After Queries的更多相关文章

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

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

  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. 【LeetCode】633. Sum of Square Numbers

    Difficulty: Easy  More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...

  4. 【LeetCode】633. Sum of Square Numbers 解题报告(python & Java & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 列表生成式 循环 日期 题目地址:https ...

  5. 【leetcode】633. Sum of Square Numbers(two-sum 变形)

    Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c. ...

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

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

  7. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  8. 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)

    [LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...

  9. 【leetcode】907. Sum of Subarray Minimums

    题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...

随机推荐

  1. Ubuntu安装openmpi

    Ubuntu 下安装 openmpi 需要同时安装下面三个包: sudo apt-get install openmpi-bin openmpi-doc libopenmpi-dev 建议更新系统源之 ...

  2. onkeyup的使用(将输入值为非数字的字符替换为空)

    onkeyup:当输入值的键盘抬起时触发这个事件. 例如: onkeyup="this.value=this.value.replace(/\D/g,'') 这是个正则式验证,用来验证输入值 ...

  3. 51nod 1490: 多重游戏(树上博弈)

    题目链接 该题实质上是一个树上博弈的问题.要定义四种状态——2先手必胜 1先手必败 3可输可赢 0不能控制 叶子结点为先手必败态: 若某结点的所有儿子都是先手必败态,则该结点为先手必胜态: 若某结点的 ...

  4. linux学习-文件管理

    1.文件系统结构 /boot:引导文件存放目录,内核文件(vmlinuz).引导加载器(bootloader, grub)都存放于此目录 /bin:所有用户使用的基本命令:不能关联至独立分区,OS启动 ...

  5. commons-fileupload-1.2.1.jar 插件上传与下载

    1:首先在页面上写个文本域: <%@ page language="java" import="java.util.*" pageEncoding=&qu ...

  6. HDU - 6601 Keen On Everything But Triangle 主席树

    Keen On Everything But Triangle 感觉最近多校好多主席树的亚子,但是本人菜得很,还没学过主席树,看着队友写题就只能划水,\(WA\)了还不能帮忙\(debug\),所以深 ...

  7. Workflow:Workflow 百科

    ylbtech-Workflow:Workflow 百科 工作流(Workflow),指“业务过程的部分或整体在计算机应用环境下的自动化”.是对工作流程及其各操作步骤之间业务规则的抽象.概括描述.在计 ...

  8. Openstack API 类型 & REST 风格

    目录 目录 Openstack 提供了三种操作方式 Web界面 CIL 指令行 RESTful API REST 风格 RESTFul风格的API设计 基于HTTP协议的RESTful API Ope ...

  9. 深入浅出HashMap

    /** *@ author ViVi *@date 2014-6-11 */ Hashmap是一种非常常用的.应用广泛的数据类型,最近研究到相关的内容,就正好复习一下.希望通过仪器讨论.共同提高~ 1 ...

  10. 把 MongoDB 当成是纯内存数据库来使用(Redis 风格)

    基本思想 将MongoDB用作内存数据库(in-memory database),也即,根本就不让MongoDB把数据保存到磁盘中的这种用法,引起了越来越多的人的兴趣.这种用法对于以下应用场合来讲,超 ...