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 = 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 解题报告的更多相关文章
- 【LeetCode】985. Sum of Even Numbers After Queries 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 找规律 日期 题目地址:https://lee ...
- #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 ...
- 【LEETCODE】47、985. Sum of Even Numbers After Queries
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【Leetcode_easy】985. Sum of Even Numbers After Queries
problem 985. Sum of Even Numbers After Queries class Solution { public: vector<int> sumEvenAft ...
- [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 ...
- 【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 ...
- 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] ...
- 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] ...
- 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)
[LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...
随机推荐
- 一些jquery特效收集
jQuery幻灯片插件带投影的图片叠加切换幻灯片轮播 特效:http://www.jsfoot.com/jquery/images/ jquery文字滚动上下间歇文字滚动 http://www.17s ...
- python初级 1 数据类型和变量
一.整数(int) 例: 0 1 2 3 -1 -2 –3 In [31]: print(type(0)) <class 'int'> In [32]: print(type(1)) & ...
- [SLAM] ***AR Tracking based on which tools?
SLAM虽然小复杂,但对于开发者而言,ar sdk通常会解决这个问题. 所以相对于识别,跟踪是个看上去高大上但实则不需要关注细节的部分. 识别,要技术深耕:跟踪,需行业深耕. 在此了解下常见的ar s ...
- [JS] Topic - define "class" by tricky methods
Ref:Javascript定义类(class)的三种方法 Javascript是一种基于对象(object-based)的语言,你遇到的所有东西几乎都是对象.但是,它又不是一种真正的面向对象编程(O ...
- DevExpress MemoEdit定位到末尾
1: /// <summary> 2: /// 追加文本到MemoEdit中 3: /// </summary> 4: /// <param name="mem ...
- echarts - 使用echarts过程中遇到的问题(pending...)
1. 配合tab切换时,被display:none的元素init设置echarts失败 2018-11-09 18:09:35 现象描述:有一个tabs选项卡,每个切换项A.B中都有使用echart ...
- mysql常用语句和函数
mysql语句如果长期不写,就会忘掉,所以要时常复习,温故而知新. 1.select length("中国人"),select char_length("中国人" ...
- day1 一、编程语言与计算机五大组成部分
一.编程与编程语言 1.什么是编程语言 语言是一个事物与另一个事物沟通的介质. 编程语言是程序员与计算机沟通的介质. 2.什么是编程 编程就是程序员按照某种编程的语法规范将自己想让计算机做的事情表达出 ...
- np.unravel_index
>>> np.unravel_index([22, 41, 37], (7,6)) (array([3, 6, 6]), array([4, 5, 1]))>>> ...
- ML.NET教程之情感分析(二元分类问题)
机器学习的工作流程分为以下几个步骤: 理解问题 准备数据 加载数据 提取特征 构建与训练 训练模型 评估模型 运行 使用模型 理解问题 本教程需要解决的问题是根据网站内评论的意见采取合适的行动. 可用 ...