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 ...
随机推荐
- 【译】Building ArduPilot on Windows with waf and Bash
原文链接:http://ardupilot.org/dev/docs/building-ardupilot-onwindows10.html 翻译水平有限,如有错误请指出! 在Windows上使用wa ...
- Windows不要使用记事本编辑文本文件
摘自:廖雪峰 千万不要使用Windows自带的记事本编辑任何文本文件.原因是Microsoft开发记事本的团队使用了一个非常弱智的行为来保存UTF-8编码的文件,他们自作聪明地在每个文件开头添加了0x ...
- iframe-父子-兄弟页面相互传值(jq和js两种方法)
参考文章: http://blog.csdn.net/u013299635/article/details/78773207 http://www.cnblogs.com/xyicheng/archi ...
- MDI容器
MDI容器 具体步骤如下: private void 销售ToolStripMenuItem_Click(object sender, EventArgs e) { VisibledForm(); F ...
- python-web自动化环境安装
web自动化环境安装 1.安装selenium 命令行使用以下命令安装selenium:pip install -U selenium 2.安装chrome浏览器 3.chromedriver的下载 ...
- Servlet 知识点总结(来自那些年的笔记)
2018年04月15日 20:16:01 淮左白衣 阅读数:350 版权声明:转载请给出原文链接 https://blog.csdn.net/youngyouth/article/details/ ...
- cookie与session的区别是什么
cookie与session的区别有:cookie以文本格式存储在浏览器上,存储量有限:而会话存储在服务端,可以无限量存储多个变量并且比cookie更安全 在php中可以指定站点的访问者信息存储在se ...
- linux 查看文件显示行号
1.用vi或vim打开文件显示行号: 显示当前行号: :nu 显示所有行号: :set nu 2.设置服务器显示行号 2.1编辑~/.vimrc文件,在该文件中加入 set nu 2.2在UBUN ...
- JavaScript: Constructor and Object Oriented Programming
Constructor : Grammar: object.constructor Example: Javascript code: 1 function obj1() { this.number ...
- python,关于用户登录与注册问题
tag=Truecount=1while tag: name = input('请输入用户名>>:').strip() password = input('请输入密码>>:') ...