【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;
        for(auto query:queries)
        {
            if(A[query[1]]%2==0 ) sum-=A[query[1]];
            A[query[1]] += query[0];
            if(A[query[1]]%2==0) sum +=A[query[1]];
            res.push_back(sum);
        }
        return res;
    }
};

参考

1. Leetcode_easy_985. Sum of Even Numbers After Queries;

原文地址:https://www.cnblogs.com/happyamyhope/p/11316944.html

时间: 2024-11-10 13:50:37

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

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

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

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 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] %

【UVA】1210 - Sum of Consecutive Prime Numbers

普通的求区间连续和的问题,一开始以为是区间移动,但是怕UVA数据太严,直接打表,后来发现自己的担心是多余的. 14044972 1210 Sum of Consecutive Prime Numbers Accepted C++ 0.049 2014-08-15 10:30:11 打表的话效率可能不是很高. AC代码: #include<cstdio> #include<cstring> #include<iostream> #include<algorithm&

【LeetCode】- Two Sum(两数相加)

[ 问题: ] Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please no

【Leetcode】Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 思路:与[Leetcode]Path Sum 不同

【HDOJ】4704 Sum

数学题.f(n) = 2^(n-1) mod (1e9+7). 1 #include <cstdio> 2 3 #define MAXN 100005 4 5 char buf[MAXN]; 6 __int64 phi = 1e9+6; 7 __int64 mod = 1e9+7; 8 9 __int64 power2(__int64 n) { 10 __int64 ret = 1, base = 2; 11 12 --n; 13 while (n) { 14 if (n & 1) 1