题目要求

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和一个query数组,query数组的每一个元素是一个列表,该列表由两个整数组成,分别对应一个值和一个A索引。要求把该值加到该索引对应的A数组的值,然后将A数组中的偶数求和。最后返回所有query对应的结果。首先对A中所有偶数求和,之后遍历query数组,判断某索引对应A中的值在加值前后的奇偶性。

python代码

class Solution:

def sumEvenAfterQueries(self, A: 'List[int]', queries: 'List[List[int]]') -> 'List[int]':

res = []

s = sum([i for i in A if i % 2 == 0])

for v, i in queries:

if A[i] % 2 == 0:

s -= A[i]

A[i] += v

if A[i] % 2 == 0:

s += A[i]

res.append(s)

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

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

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

  4. 【Leetcode_easy】985. Sum of Even Numbers After Queries

    problem 985. Sum of Even Numbers After Queries class Solution { public: vector<int> sumEvenAft ...

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

  6. 【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 ...

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

  8. 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] ...

  9. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

随机推荐

  1. Unity和Android混合开发

    通用的流程 https://blog.csdn.net/zhangdi2017/article/details/65629589 应用场景 Unity游戏中一些功能需要安卓系统的支持,如搜索wifi等 ...

  2. c++ 出现“ error LNK2019: 无法解析的外部符号 该符号在函数 中被引用"错误原因

    一般问题出在 (1)XXX.lib头文件,这个要包含(不然编译也不能通过) (2)需要XXX.lib或XXX.dll库.手动添加,项目->属性->配置属性->链接器->输入 然 ...

  3. 【转帖】39个让你受益的HTML5教程

    39个让你受益的HTML5教程                    闲话少说,本文作者为大家收集了网上学习HTML5的资源,期望它们可以帮助大家更好地学习HTML5. 好人啊! 不过,作者原来说的4 ...

  4. Schema中elementFormDefault="qualified"所起的作用

    有的xsd文件的开头是这样写的: <?xml version="1.0"?> <xs:schema xmlns:xs="http://www.w3.or ...

  5. Tomcat容器做到自我保护,设置最大连接数(服务限流:tomcat请求数限制)

    http://itindex.net/detail/58707-%E5%81%87%E6%AD%BB-tomcat-%E5%AE%B9%E5%99%A8 为了确保服务不会被过多的http长连接压垮,我 ...

  6. EventFlow.helper.js 事件流程控制

    /*! * 事件流程管理 * version: 1.0.0-2018.07.25 * Requires ES6 * Copyright (c) 2018 Tiac * http://www.cnblo ...

  7. php info

    http://www.cnblogs.com/xiaochaohuashengmi/archive/2010/08/12/1797753.html  php pdo 相关 http://blog.cs ...

  8. Android 信号处理面面观 之 信号定义、行为和来源

    总结: Android中: Sending signal. PID: XXX SIG: 3   ====>打印trace 原文:http://blog.csdn.net/rambo2188/ar ...

  9. svn eclipse链接

    先下载site-1.8.22.zip 安装包 然后 在D:\software\eclipse\dropins 目录下新建 svn文件夹 把下载的文件解压到该文件夹下 ,*.xml 删除 不需要 只要 ...

  10. C#自定义Winform无边框窗体

    C#自定义Winform无边框窗体 在实际项目中,WinForm窗体或者控件不能满足要求,所以就需要自己设计窗体等,当然设计界面可以用的东西很多,例如WPF.或者一些第三方的库等.本例中将采用WinF ...