Sum of Even Numbers After Queries LT985
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 int[] sumEvenAfterQueries(int[] A, int[][] queries) {
int sum = 0;
int[] result = new int[queries.length];
for(int a: A) {
if((a&1) == 0) {
sum += a;
}
} for(int i = 0; i < queries.length; ++i) {
int[] query = queries[i];
int index = query[1];
int val = query[0];
if((A[index]&1) == 0) {
if((val&1) == 0) {
sum+= val;
}
else {
sum -= A[index];
}
}
else {
if((val&1) == 1) {
sum += val + A[index];
}
}
A[index] += val;
result[i] = sum;
} return result;
}
}
看到官方解法才感觉上面的解法笨,好好看题,只有even value matters, new value replaced by one one, if old one is even, deduct it, if new value is even, add it, simple as it is, no need to check all the combinations of even/odd cases.
class Solution {
public int[] sumEvenAfterQueries(int[] A, int[][] queries) {
int sum = 0;
int[] result = new int[queries.length];
for(int a: A) {
if((a&1) == 0) {
sum += a;
}
} for(int i = 0; i < queries.length; ++i) {
int[] query = queries[i];
int index = query[1];
int val = query[0]; if((A[index] & 1) == 0) {
sum -= A[index];
}
A[index] += val;
if((A[index] & 1) == 0) {
sum += A[index];
}
result[i] = sum;
} return result;
}
}
Sum of Even Numbers After Queries LT985的更多相关文章
- 【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 ...
- [Swift]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] ...
- 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
https://leetcode.com/problems/sum-of-even-numbers-after-queries/ We have an array A of integers, and ...
- 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 ...
- 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 ...
随机推荐
- RouterOS限速更高级运用
转自这里 一般我们用ros限速只是使用了max-limit,其实ros限速可以更好的运用.比如我们希望客户打开网页时速度可以快一些,下载时速度可以慢一些.ros2.9就可以实现. 看图片 max-li ...
- 在WINDOWS下安装MYSQL8.0
1:创建文件夹D:\data\service\mysql-8.0.11-winx64\data 2:进到D:\data\service\mysql-8.0.11-winx64\bin 第三步:初始化. ...
- 涂抹mysql笔记-InnoDB/MyISAM及其它各种存储引擎
存储引擎:一种设计的存取和处理方式.为不同访问特点的表对象指定不同的存储引擎,可以获取更高的性能和处理数据的灵活性.通常是.so文件,可以用mysql命令加载它. 查看当前mysql数据库支持的存储引 ...
- [SQL]某数据库中查出包含 字段名 的所有表名
--利用SQL语句来查询字段所在的表 --从某数据库中查出包含 字段名 字段的所有表名 SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE ...
- 通过eclipse打开jdk native源码
1.下载 eclipse http://www.eclipse.org/downloads/eclipse-packages/ 建议下载 Eclipse IDE for Eclipse Committ ...
- 用Jedis调用Lua脚本来完成redis的数据操作
1.先完成一个简单的set/get操作 package com.example.HnadleTaskQueue; import redis.clients.jedis.Jedis; import ja ...
- js 一些方法
1.js去除字符串前后的空格 function Trim(str) { return str.replace(/(^\s*)|(\s*$)/g, ""); } 2.js打乱数组的顺 ...
- win10家庭版 监听无法启动 报TNS-12560 TNS-00530
首先win10权限问题, 搜索设置->更新和安全 ->恢复->高级启动立即重启 疑难解答-高级选项-启动设置-重启-选择“4” 按“WIN+R”组合键,输入“control user ...
- 基于LNMP的Zabbix4.0.1部署
转:http://www.safecdn.cn/monitor/2018/12/lnmp-zabbix4-0-1-install/306.htmlZabbix4.0.1部署 一 安装源和Zabb ...
- 反序列化失败Failed to deserialize --- local class incompatible: stream classdesc serialVersionUID
反序列化失败: java.lang.IllegalStateException: Failed to execute CommandLineRunner at org.springframework. ...