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] % 2 == 0 else 0
            ans.append(s)
            A[i] = new
        return ans

原文地址:https://www.cnblogs.com/zywscq/p/10699778.html

时间: 2024-08-29 12:24:18

Leetcode 985. Sum of Even Numbers After Queries的相关文章

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 inde

【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 OJ - Sum Root to Leaf Numbers

这道题也很简单,只要把二叉树按照宽度优先的策略遍历一遍,就可以解决问题,采用递归方法越是简单. 下面是AC代码: 1 /** 2 * Sum Root to Leaf Numbers 3 * 采用递归的方法,宽度遍历 4 */ 5 int result=0; 6 public int sumNumbers(TreeNode root){ 7 8 bFSearch(root,0); 9 return result; 10 } 11 private void bFSearch(TreeNode ro

[leetcode]Combination Sum @ Python

原题地址:https://oj.leetcode.com/problems/combination-sum/ 题意: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited