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 = 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.

class Solution {
public:
  vector<int> sumEvenAfterQueries(vector<int>& A, vector<vector<int>>& queries) {
    int initval = 0;
    vector<int> ret;
    for(int v : A) {
      if(v % 2 == 0) initval += v;
    }
    for(auto v : queries) {
      int before = A[v[1]];
      A[v[1]] += v[0];
      if(before % 2 == 0) initval -= before;
      if(A[v[1]] % 2 == 0) initval += A[v[1]];
      ret.push_back(initval);
    }
    return ret;
  }
};

原文地址:https://www.cnblogs.com/ethanhong/p/10350107.html

时间: 2024-11-13 09:41:03

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

【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

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

[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

Timus 1120. Sum of Sequential Numbers 数学题

There is no involute formulation concerning factitiously activity of SKB Kontur in this problem. Moreover, there is no formulation at all. Input There is the only number N, 1 ≤ N ≤ 109. Output Your program is to output two positive integers A and P s

[LC] 129. Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. Note: A leaf is a node

1-Two sum 2-Add two numbers

1.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