题目要求

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. pycharm如何在debug的时候动态执行python语句

    在调试MATLAB的时候,这一点很容易实现,比如动态修改变量的值,在VS2017中调试python程序,这一点也很容易实现,但是我在pycharm里面找了半天,如下图:

  2. 删除最少字符生成Palindrome

    看到一个哥们G家电面试题,求删除最少字符生成Palindrome.大神们都在讨论dp,我完全想不到. 放一个没优化的DFS. 乐神说是O(n3)复杂度, 有机会的话要减少使用substring,并且要 ...

  3. java.lang.String.regionMatches方法使用

    regionMatches(boolean ignoreCase,int toffset,String other,int ooffset,int len): regionMatches(int to ...

  4. Java知多少(85)文本框和文本区

    在图形界面中,文本框和文本区是用于信息输入输出的组件. 文本框 文本框(JTextField)是界面中用于输入和输出一行文本的框.JTextField类用来建立文本框.与文本框相关的接口是Action ...

  5. callback 回调函数

    把函数a当做一个参数传入函数b <script> Array.prototype.mysort = function(callback){ let s = this; //准备向回调函数里 ...

  6. ubuntu14.04 中virtual box 加入 usb

    参考两篇文章: 1.http://www.cnblogs.com/ericsun/archive/2013/06/10/3130679.html 2.http://www.360doc.com/con ...

  7. R语言三元相图的做法

    通常情况下,对于三维数据,我们会用三维图表来展示,想要从三维图表上观察出一定的规律,需要一定的空间想象力: 而三元相图,其实就是用二维平面的1个等边三角形来表征三维数据,三角形的每一条边对应1个维度, ...

  8. excel中批量删除公式,保留数值

    excel中批量删除公式,保留数值 Sub macro1() Dim sh As Worksheet For Each sh In Sheets sh.UsedRange = sh.UsedRange ...

  9. [PHP] 05 - Cookie & Session

    故事背景 同 http, html, REST API 一样属于基础性的知识内容. [Node.js] 07 - Html and Http [Node.js] 08 - Web Server and ...

  10. js中的XMLHTTPRequest

    window.onload = function(){ //var url = "http://localhost:8000/sales.json"; var url = &quo ...