码迷,mamicode.com
首页 > 其他好文 > 详细

LC 985. Sum of Even Numbers After Queries

时间:2019-02-03 14:05:23      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:span   rman   ber   vector   pac   font   back   code   solution   

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;
  }
};

 

LC 985. Sum of Even Numbers After Queries

标签:span   rman   ber   vector   pac   font   back   code   solution   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!