https://leetcode.com/problems/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.

Example 1:

Input: A = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]]
Output: [8,6,2,4]
Explanation:
At the beginning, the array is [1,2,3,4].
After adding 1 to A[0], the array is [2,2,3,4], and the sum of even values is 2 + 2 + 4 = 8.
After adding -3 to A[1], the array is [2,-1,3,4], and the sum of even values is 2 + 4 = 6.
After adding -4 to A[0], the array is [-2,-1,3,4], and the sum of even values is -2 + 4 = 2.
After adding 2 to A[3], the array is [-2,-1,3,6], and the sum of even values is -2 + 6 = 4.

Note:

  1. 1 <= A.length <= 10000
  2. -10000 <= A[i] <= 10000
  3. 1 <= queries.length <= 10000
  4. -10000 <= queries[i][0] <= 10000
  5. 0 <= queries[i][1] < A.length

代码:

class Solution {
public:
vector<int> sumEvenAfterQueries(vector<int>& A, vector<vector<int>>& queries) {
int na = A.size(), nq = queries.size();
int sum = 0;
vector<int> ans;
for(int i = 0; i < na; i ++) {
if(A[i] % 2 == 0) sum += A[i];
} for(int i = 0; i < nq; i ++) {
int pos = queries[i][1], val = queries[i][0];
if(A[pos] % 2 == 0) {
if((A[pos] + val) % 2 == 0)
sum += val;
else sum -= A[pos];
} else {
if((A[pos] + val) % 2 == 0)
sum += val + A[pos];
}
A[pos] += val;
ans.push_back(sum);
}
return ans;
}
};

  

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

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

  2. 【LEETCODE】47、985. Sum of Even Numbers After Queries

    package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...

  3. 【Leetcode_easy】985. Sum of Even Numbers After Queries

    problem 985. Sum of Even Numbers After Queries class Solution { public: vector<int> sumEvenAft ...

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

  5. 【LeetCode】985. Sum of Even Numbers After Queries 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 找规律 日期 题目地址:https://lee ...

  6. 【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 = quer ...

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

  8. 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] ...

  9. LeetCode.985-查询后偶数的总和(Sum of Even Numbers After Queries)

    这是悦乐书的第370次更新,第398篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第232题(顺位题号是985).有一个整数数组A和一个查询数组queries. 对于第i ...

随机推荐

  1. SQLServer数据库增删改查

    一.数据库定义 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库.数据库的操作分为两种形式:一种是直接在数据库管理工具图形化界面进行操作:一种是使用数据库脚本进行操作,数据库脚本可 ...

  2. 【Teradata】日期类型转换

    1.字符串与日期间转换 date '2007-05-10' cast( (curent_timestamp() (format )) //结果为20180615164201 2.毫秒转换为时间戳 / ...

  3. winfrom之datagridview分页显示

    这次datagridview绑定数据并分页操作,因为用到了webservice,所以代码会详细讲解.QueryByCondition是一个查询函数 客户端: PageData pageData=new ...

  4. JavaScript如何计算两个日期间的时间差

    <script type="text/javascript"> /* * 获得时间差,时间格式为 年-月-日 小时:分钟:秒 或者 年/月/日 小时:分钟:秒 * 其中 ...

  5. Error response from daemon: rpc error: code = Unknown desc = name conflicts with

    环境:centos7 执行一下命令时, docker service create --mode global --name logspout gliderlabs/logspout 出现以下报错: ...

  6. 【vue-waring】element UI 由版本1.4.12 升级到element-ui@2.0.10

    遇到的问题:element UI   由版本1.4.12 升级到element-ui@2.0.10    cnpm run dev 运行后的waring 状态:解决(相关资料的方法对我没什么用) 解决 ...

  7. 【angularJS】过滤器

    1.分类: <1>内置过滤器(见4) <2>自定义过滤器 2.作用:接收一个输入,通过某个规则进行处理,然后返回处理后的结果 3.应用: <1>在模板中使用 用法 ...

  8. 解决 Vim 的 quickfix 插件错误信息乱码问题

      将以下代码插入 vim 配置文件即可,       function! QfMakeConv()        let qflist = getqflist()        for i in q ...

  9. Echo团队Alpha冲刺随笔 - 第五天

    项目冲刺情况 进展 前端:布局,内容等方面基本完成. 后端:基本功能基本实现. 计划:准备进行前后端对接,进行测试 问题 有部分代码冗余,需要着手修改 心得 团队分工明确,互相协作,开发进度比预想的要 ...

  10. Python通过pip方式安装第三方模块的两种方式

    一:环境 python3.6 windows 10 二:常用命令 如果直接执行pip命令报错,说明pip不在path环境变量中 解决方法: python -m pip list 以下默认可直接使用pi ...