#Leetcode# 985. Sum of Even Numbers After Queries
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 <= A.length <= 10000
-10000 <= A[i] <= 10000
1 <= queries.length <= 10000
-10000 <= queries[i][0] <= 10000
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的更多相关文章
- 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 ...
- 【LEETCODE】47、985. Sum of Even Numbers After Queries
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- 【Leetcode_easy】985. Sum of Even Numbers After Queries
problem 985. Sum of Even Numbers After Queries class Solution { public: vector<int> sumEvenAft ...
- [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 ...
- 【LeetCode】985. Sum of Even Numbers After Queries 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 找规律 日期 题目地址:https://lee ...
- 【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 ...
- 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] ...
- 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] ...
- LeetCode.985-查询后偶数的总和(Sum of Even Numbers After Queries)
这是悦乐书的第370次更新,第398篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第232题(顺位题号是985).有一个整数数组A和一个查询数组queries. 对于第i ...
随机推荐
- Tomcat配置自定义JAVA环境
Tomcat的bin目录下 在setclasspath.sh(Linux系统,Windows系统为setclasspath.bat)文件的开头添加: export JAVA_HOME=/usr/lib ...
- JS一些实用的方法
1.首次为变量赋值时务必使用var关键字 变量没有声明而直接赋值得话,默认会作为一个新的全局变量,要尽量避免使用全局变量. 2.使用===取代== ==和!=操作符会在需要的情况下自动转换数据类型.但 ...
- win10安装nodejs遇到提示错误代码2503怎么办
我们在安装某个软件的时候,最闹心的就是遇到提示安装失败或错误,比如win10系统在安装nodejs遇到提示错误代码2503,遇见这个问题也不要慌张,今天小编就来告诉大家怎么解决这个问题. 1.打开智能 ...
- Ubuntu18.04多个版本GCC编译器的切换
今天make一个程序的时候,发现程序里面使用到了C++17的标准,而我的gcc仍然是4.8,考虑到系统是ubuntu18.04的,所以感觉应该gcc的版本不会这么低. cd到/usr/bin下,使用指 ...
- 【算法】LeetCode算法题-Length Of Last Word
这是悦乐书的第155次更新,第157篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第14题(顺位题号是58).给定一个字符串,包含戴尔字母.小写字母和空格,返回最后一个单 ...
- 阿里云CentOS下nodejs安装
1. 下载node包(包含npm) cd /usr/local/src/ wget https://nodejs.org/dist/v10.11.0/node-v10.11.0-linux-x64.t ...
- Loj #6069. 「2017 山东一轮集训 Day4」塔
Loj #6069. 「2017 山东一轮集训 Day4」塔 题目描述 现在有一条 $ [1, l] $ 的数轴,要在上面造 $ n $ 座塔,每座塔的坐标要两两不同,且为整点. 塔有编号,且每座塔都 ...
- centos7下源码安装多个nginx步骤完整版
1.下载:wget http://nginx.org/download/nginx-1.10.0.tar.gz 解压:tar -zxvf nginx-1.10.0.tar.gz 2. 执行下面 ...
- 安装Anaconda 之后使用ubuntu自带python
我们知道,Ubantu系统会自带python,当你在terminal窗口中输入python,就会显示默认安装的python的信息.比如我的16.04就自带了python2.7和3.5,但是安装了Ana ...
- html 传递参数中文乱码 js获取参数乱码
每天学习一点点 编程PDF电子书.视频教程免费下载:http://www.shitanlife.com/code HTML传递中文参数时,有乱码导致接收不到正确的数据.JS中可以使用encodeURI ...