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

原文地址:https://www.cnblogs.com/yao1996/p/10356224.html

时间: 2024-08-29 01:25:27

LeetCode 985 Sum of Even Numbers After Queries 解题报告的相关文章

Leetcode 985. Sum of Even Numbers After Queries

简单题,按照它给的说法计算就行. class Solution: def sumEvenAfterQueries(self, A: List[int], queries: List[List[int]]) -> List[int]: s = sum(x for x in A if x % 2 == 0) ans = [] for v, i in queries: new = A[i] + v s += new if new % 2 == 0 else 0 s -= A[i] if A[i] %

【Leetcode_easy】985. Sum of Even Numbers After Queries

problem 985. Sum of Even Numbers After Queries class Solution { public: vector<int> sumEvenAfterQueries(vector<int>& A, vector<vector<int>>& queries) { vector<int> res; int sum = 0; for(auto a:A) if(a%2==0) sum +=a; f

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

【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 ind

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][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 = q

[Swift Weekly Contest 122]LeetCode985. 查询后的偶数和 | 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 = q

LeetCode 633. Sum of Square Numbers平方数之和 (C++)

题目: Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c. Example 1: Input: 5 Output: True Explanation: 1 * 1 + 2 * 2 = 5 Example 2: Input: 3 Output: False 分析: 给定一个非负整数c ,你要判断是否存在两个整数a和b,使

【LeetCode】Find Minimum in Rotated Sorted Array 解题报告

今天看到LeetCode OJ题目下方多了"Show Tags"功能.我觉着挺好,方便刚開始学习的人分类练习.同一时候也是解题时的思路提示. [题目] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no

LeetCode Roman to Integer 罗马字符转数字 解题报告

https://oj.leetcode.com/problems/roman-to-integer/ Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 把一个给定的罗马字符转为数字.首先要了解罗马字符表示的规则. 一,羅馬數字共有7個,即I(1).V(5).X(10).L(50).C(100).D(500)和M(1000). 二,在