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] <= 100001 <= queries.length <= 10000-10000 <= queries[i][0] <= 100000 <= 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 ...
随机推荐
- 自定义python扩展类型
目标:自定义一个C\C++矩阵类,有几个用于演示的矩阵运算函数或者操作,将其通过 PyTypeOject newType的方式注册到python中成为一种新的类型,并且要可继承. 预备知识 建议先运行 ...
- C++Primer第五版——习题答案详解(四)
习题答案目录:https://www.cnblogs.com/Mered1th/p/10485695.html 第5章 语句 练习5.9 #include<iostream> #inclu ...
- 基于keras的fasttext短文本分类
### train_model.py ### #!/usr/bin/env python # coding=utf-8 import codecs import simplejson as json ...
- Jobs深入学习
代码回顾 Quartz 需要了解你可能希望该作业的实例拥有的各种属性,这是通过JobDetail 类完成的. JobDetail 实例是使用 JobBuilder 类构建的. JobDetail j ...
- 第26课 可变参数模板(7)_any和variant类的实现
1. any类的实现 (1)any类: ①是一个特殊的,只能容纳一个元素的容器,它可以擦除类型,可以将何任类型的值赋值给它. ②使用时,需要根据实际类型将any对象转换为实际的对象. (2)实现any ...
- linux服务之apache篇(一)
1.apache介绍:使用率最高的网站服务器: URL:统一资源定位符: 端口:http:80 https:443 2.apache三种工作模式: prefork:一个线程处理一个请求(占用内存多 ...
- PHP运行出现Notice : Use of undefined constant 的解决办法
这些是 PHP 的提示而非报错,PHP 本身不需要事先声明变量即可直接使用,但是对未声明变量会有提示.一般作为正式的网站会把提示关掉的,甚至连错误信息也被关掉 关闭 PHP 提示的方法 搜索php.i ...
- atom常用插件
汉化 simplified-chinese-menureact atom-react-snippets-0.5.0polymer atom-polymer-0.13.0polymer Atom-Pol ...
- python3中的urllib.parse的常用方法
将URL按一定的格式进行拆分 使用 urllib.parse.urlparse将url分为6个部分,返回一个包含6个字符串项目的元组:协议.位置.路径.参数.查询.片段 参照官方地址:https:// ...
- jQuery.ajax()调用asp.net后台方法(非常重要)
http://www.cnblogs.com/zxhoo/archive/2011/01/30/1947752.html 用JQuery的$.ajax()可以很方便的调用asp.net的后台方法. 先 ...