当前位置:网站首页>LeetCode每日一题(985. Sum of Even Numbers After Queries)
LeetCode每日一题(985. Sum of Even Numbers After Queries)
2022-07-03 09:01:00 【wangjun861205】
You are given an integer array nums and an array queries where queries[i] = [vali, indexi].
For each query i, first, apply nums[indexi] = nums[indexi] + vali, then print the sum of the even values of nums.
Return an integer array answer where answer[i] is the answer to the ith query.
Example 1:
Input: nums = [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 nums[0], the array is [2,2,3,4], and the sum of even values is 2 + 2 + 4 = 8.
After adding -3 to nums[1], the array is [2,-1,3,4], and the sum of even values is 2 + 4 = 6.
After adding -4 to nums[0], the array is [-2,-1,3,4], and the sum of even values is -2 + 4 = 2.
After adding 2 to nums[3], the array is [-2,-1,3,6], and the sum of even values is -2 + 6 = 4.
Example 2:
Input: nums = [1], queries = [[4,0]]
Output: [0]
Constraints:
- 1 <= nums.length <= 104
- -104 <= nums[i] <= 104
- 1 <= queries.length <= 104
- -104 <= vali <= 104
- 0 <= indexi < nums.length
考虑 4 种情况:
- nums[i]原来是偶数, 加上 val 之后还是偶数, 这样偶数的和增加了 val
- nums[i]原来是偶数, 加上 val 之后变成奇数, 这样偶数的和减少了 nums[i]
- nums[i]原来是奇数, 加上 val 之后变成偶数, 这样偶数的和增加了 nums[i] + val
- nums[i]原来是奇数, 加上 val 之后还是奇数, 这样偶数的和没有任何变化
impl Solution {
pub fn sum_even_after_queries(mut nums: Vec<i32>, queries: Vec<Vec<i32>>) -> Vec<i32> {
let mut sum = nums.iter().filter(|v| *v % 2 == 0).map(|v| *v).sum();
let mut ans = Vec::new();
for q in queries {
let curr = nums[q[1] as usize];
if curr % 2 == 0 {
if (curr + q[0]) % 2 == 0 {
sum += q[0];
} else {
sum -= curr;
}
} else {
if (curr + q[0]) % 2 == 0 {
sum += curr + q[0];
}
}
nums[q[1] as usize] += q[0];
ans.push(sum);
}
ans
}
}
边栏推荐
- LeetCode每日一题(1162. As Far from Land as Possible)
- Spark 集群安装与部署
- Beego learning - JWT realizes user login and registration
- Install third-party libraries such as Jieba under Anaconda pytorch
- Hudi学习笔记(三) 核心概念剖析
- 2022-2-13 learn the imitation Niuke project - Project debugging skills
- [kotlin puzzle] what happens if you overload an arithmetic operator in the kotlin class and declare the operator as an extension function?
- [point cloud processing paper crazy reading classic version 7] - dynamic edge conditioned filters in revolutionary neural networks on Graphs
- 2022-2-14 learning xiangniuke project - Session Management
- Vs2019 configuration opencv3 detailed graphic tutorial and implementation of test code
猜你喜欢

【点云处理之论文狂读经典版10】—— PointCNN: Convolution On X-Transformed Points

How to check whether the disk is in guid format (GPT) or MBR format? Judge whether UEFI mode starts or legacy mode starts?

Hudi 快速体验使用(含操作详细步骤及截图)

State compression DP acwing 291 Mondrian's dream

AcWing 786. Number k
![[point cloud processing paper crazy reading classic version 14] - dynamic graph CNN for learning on point clouds](/img/7d/b66545284d6baea2763fd8d8555e1d.png)
[point cloud processing paper crazy reading classic version 14] - dynamic graph CNN for learning on point clouds

Spark 概述

Hudi 数据管理和存储概述
![[point cloud processing paper crazy reading frontier version 8] - pointview gcn: 3D shape classification with multi view point clouds](/img/ee/3286e76797a75c0f999c728fd2b555.png)
[point cloud processing paper crazy reading frontier version 8] - pointview gcn: 3D shape classification with multi view point clouds

We have a common name, XX Gong
随机推荐
【点云处理之论文狂读经典版13】—— Adaptive Graph Convolutional Neural Networks
Filter comments to filter out uncommented and default values
Use the interface colmap interface of openmvs to generate the pose file required by openmvs mvs
[point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks
Beego learning - JWT realizes user login and registration
Using Hudi in idea
Overview of image restoration methods -- paper notes
PowerDesigner does not display table fields, only displays table names and references, which can be modified synchronously
Principles of computer composition - cache, connection mapping, learning experience
[point cloud processing paper crazy reading classic version 8] - o-cnn: octree based revolutionary neural networks for 3D shape analysis
Idea uses the MVN command to package and report an error, which is not available
Powerdesign reverse wizard such as SQL and generates name and comment
How to check whether the disk is in guid format (GPT) or MBR format? Judge whether UEFI mode starts or legacy mode starts?
What are the stages of traditional enterprise digital transformation?
Basic knowledge of database design
【点云处理之论文狂读前沿版11】—— Unsupervised Point Cloud Pre-training via Occlusion Completion
Computing level network notes
[point cloud processing paper crazy reading cutting-edge version 12] - adaptive graph revolution for point cloud analysis
2022-1-6 Niuke net brush sword finger offer
LeetCode每日一题(1300. Sum of Mutated Array Closest to Target)