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 ...
随机推荐
- .NET 定时执行任务解决方案(Timer & Quartz.Net)
共有两种方法: 一.使用Timer global.asax <%@ Application Language="C#" %> <%@ import Namespa ...
- 对于HTML页面中CSS, JS, HTML的加载与执行过程的简单分析
来自 https://blog.csdn.net/u011088260/article/details/79563315 最近在研究HTML页面中JavaScript的执行顺序问题.在Java ...
- FastCGI点滴
FastCGI是一种二进制协议,用于将交互式程序与Web服务器连接.它是早期通用网关接口(CGI)的变体.FastCGI的主要目标是减少与Web服务器和CGI程序之间的接口相关的开销,允许服务器每单位 ...
- 读书笔记——《redis入门指南(第2版)》第三章 入门
3 .redis的5种数据类型及相应命令 redis不区分命令大小写. string 512m 一个散列类型键可包含至多232-1个字段 一个列表类型键最多能容纳232-1个元素 一个集合类型键最多能 ...
- Excel清除无用数据行和数据列
http://jingyan.baidu.com/article/6525d4b13ae608ac7c2e9478.html ctrl+shift+↓ ctrl+- ctrl+shift+→ ctrl ...
- jmeter单sql语句测试
前提:在进行接口或者性能测试时需要用到数据库连接,此文讲解简单的单sql语句执行 步骤1:启动jmeter,新建一个测试计划,新建一个Thread(此处不作详细说明) 步骤2:再新建一个JDBC Co ...
- oracle自定义函数返回结果集
首先要弄两个type,不知道什么鬼: 1. create or replace type obj_table as object ( id ), name ), ) ) 2. create or re ...
- [UnityShader基础]04.ColorMask
语法如下: ColorMask RGB | A | 0 | 其他R,G,B,A的组合 ColorMask R,意思是输出颜色中只有R通道会被写入 ColorMask 0,意思是不会输出任何颜色 默认值 ...
- 清除UIWebView缓存
//清除cookies NSHTTPCookie *cookie; NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCook ...
- 清除eclipse,STS workspace历史记录
记一下 打开eclipse下的/configuration/.settings目录 修改文件org.eclipse.ui.ide.prefs文件 把RECENT_WORKSPACES这项修改为你需要的 ...