【leetcode】985. Sum of Even Numbers After Queries
题目如下:
We have an array
Aof integers, and an arrayqueriesof queries.For the
i-th queryval = queries[i][0], index = queries[i][1], we add val toA[index]. Then, the answer to thei-th query is the sum of the even values ofA.(Here, the given
index = queries[i][1]is a 0-based index, and each query permanently modifies the arrayA.)Return the answer to all queries. Your
answerarray should haveanswer[i]as the answer to thei-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 <= A.length <= 10000-10000 <= A[i] <= 100001 <= queries.length <= 10000-10000 <= queries[i][0] <= 100000 <= queries[i][1] < A.length
解题思路:题目很简单,有一点需要注意,就是每次执行query后,不需要遍历整个数组求出所有偶数的值。只需要记录上一次偶数的和,执行query前,如果该值是偶数,用上一次的和减去该值;执行query后,如果新值是偶数,用上一次的和加上这个新值,就可以得到这次query执行后的偶数总和。
代码如下:
class Solution(object):
def sumEvenAfterQueries(self, A, queries):
"""
:type A: List[int]
:type queries: List[List[int]]
:rtype: List[int]
"""
res = []
count = None
for val,inx in queries:
before = A[inx]
A[inx] += val
after = A[inx]
if count == None:
count = sum(filter(lambda x: x % 2 == 0,A))
else:
if before % 2 == 0:
count -= before
if after % 2 == 0:
count += after
res.append(count)
return res
【leetcode】985. Sum of Even Numbers After Queries的更多相关文章
- 【LeetCode】985. Sum of Even Numbers After Queries 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 找规律 日期 题目地址:https://lee ...
- 【Leetcode_easy】985. Sum of Even Numbers After Queries
problem 985. Sum of Even Numbers After Queries class Solution { public: vector<int> sumEvenAft ...
- 【LeetCode】633. Sum of Square Numbers
Difficulty: Easy More:[目录]LeetCode Java实现 Description https://leetcode.com/problems/sum-of-square-n ...
- 【LeetCode】633. Sum of Square Numbers 解题报告(python & Java & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 列表生成式 循环 日期 题目地址:https ...
- 【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. ...
- 【LEETCODE】47、985. Sum of Even Numbers After Queries
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)
[LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...
- 【leetcode】907. Sum of Subarray Minimums
题目如下: 解题思路:我的想法对于数组中任意一个元素,找出其左右两边最近的小于自己的元素.例如[1,3,2,4,5,1],元素2左边比自己小的元素是1,那么大于自己的区间就是[3],右边的区间就是[4 ...
随机推荐
- cf1278D——树的性质+并查集+线段树/DFS判环
昨天晚上本来想认真打一场的,,结果陪女朋友去了.. 回来之后看了看D,感觉有点思路,结果一直到现在才做出来 首先对所有线段按左端点排序,然后用并查集判所有边是否联通,即遍历每条边i,和前一条不覆盖它的 ...
- Delphi读取和写入utf-8编码格式的文件
读取UTF-8格式的文件内容 function LoadUTF8File(AFileName: string): string; var ffileStream:TFileStream; fAnsiB ...
- CocoaPods中pod search报错的解决办法
pod search报错的原因: 每次使用pod search命令的使用会在该目录下生成一个search_index.json文件,会逐渐的增大,文件达到一定大小后,ruby解析里面的json字符就会 ...
- [CSP-S模拟测试]:water(BFS)
题目描述 有一块矩形土地被划分成$n\times m$个正方形小块.这些小块高低不平,每一小块都有自己的高度.水流可以由任意一块地流向周围四个方向的四块地中,但是不能直接流入对角相连的小块中.一场大雨 ...
- iOS 开发加密做法
一般做法是这样的: 客户端 每一个请求的URL中加上时间的参数.对url中的参数是排序好的. 然后对这个URL进行MD5.将这个MD5作为最后一个参数(sign)拼接到url最后. 服务端 收到请求后 ...
- 第一步:卸载手机上的“WIFI万能钥匙”! 会分享自己家的wifi
http://www.znds.com/tv-164866-1-1.html 楼主今日家中WIFI卡的不行,心想,20M的电信光纤没理由这么卡!于是就在网上查找了许多资料,接下来统一收集整理给大家!第 ...
- 通过TCP/IP连接Mysql数据库
问题:mysql只能用localhost或127.0.0.1连接 解决:mysql安装完后,默认是root用户,root用户只能在服务器登录,需要分配新用户. 1.以root用户登陆mysql数据库. ...
- MFC绘图基础
·MFC中三种坐标系统: 1.屏幕坐标系 坐标原点位于屏幕左上角 2.(非客户区)窗口坐标系 坐标原点位于窗口左上角(包括标题栏) 3.客户区坐标系 坐标原点位于客户区左上角(不包括标题栏) ·坐标系 ...
- mysql中@ROW的使用
一.从test02表中查询每个uid最早登录的前2天 表如下图所示: select * from (select uid,day, @ROW := END rn, @uuid:=uid from (s ...
- 关于deepin下安装ssh以后root用户登陆报错的解决
最近刚刚接触到deepin,觉得,wow,除了mac,还有这么好看的非win系统,而且第测出那个Linux,宽容度很高,非常适合我这种比较喜欢折腾的人,于是下载了deepin15版本并将其当作虚拟机成 ...