985. Sum of Even Numbers After Queries
We have an array
Aof integers, and an arrayqueriesof queries.For the
i-th queryval = queries[i][0], index = queries[i][1], we add val toA[index]. Then, the answer to thei-th query is the sum of the even values ofA.(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
answerarray should haveanswer[i]as the answer to thei-th query.有两个数组A和queries,queries是个二维数组,第一个元素是值,第二个元素是索引,循环queries数组,把queries的每个值加到A数组对应索引位置上。求出每一次循环后A数组中的偶数和。
思路:先求出A数组中的偶数和。再循环queries数组,共四种情况:加运算之前是偶数,之后是奇数:总和-旧值;偶数-》偶数:总和+新值;奇数-》偶数:总和+旧值+新值;奇数-》奇数:总和不变。
class Solution {
public int[] sumEvenAfterQueries(int[] A, int[][] queries) {
int[] result = new int[queries.length];
int evenSum = 0;
for (int i1 : A) {
if (i1 % 2 == 0) {
evenSum += i1;
}
}
for (int i = 0; i < queries.length; i++) {
int index = queries[i][1];
int val = queries[i][0];
int old = A[index];
A[index] = A[index] + val;
if (old % 2 == 0 && A[index] % 2 != 0) {
result[i] = evenSum - old;
evenSum = result[i];
continue;
}
if (old % 2 == 0 && A[index] % 2 == 0) {
result[i] = evenSum + val;
evenSum = result[i];
continue;
}
if (old % 2 != 0 && A[index] % 2 == 0) {
result[i] = evenSum + old + val;
evenSum = result[i];
continue;
}
if (old % 2 != 0 && A[index] % 2 != 0) {
result[i] = evenSum;
evenSum = result[i];
}
}
return result;
}
}
985. Sum of Even Numbers After Queries的更多相关文章
- 【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 ...
- #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
题目如下: We have an array A of integers, and an array queries of queries. For the i-th query val = quer ...
- 【LeetCode】985. Sum of Even Numbers After Queries 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力 找规律 日期 题目地址:https://lee ...
- LeetCode.985-查询后偶数的总和(Sum of Even Numbers After Queries)
这是悦乐书的第370次更新,第398篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第232题(顺位题号是985).有一个整数数组A和一个查询数组queries. 对于第i ...
随机推荐
- Linux运维小知识
自己日常用到的命令稍微备份一下: 版本确认 CentOS / RedHat Enterprise cat /etc/redhat-release Ubuntu cat /etc/lsb-release ...
- goflyway简单使用
前言 一个朋友最近新买的搬瓦工ip突然被强了,要等10周左右才能免费更换ip.而恰巧在网上看到了Goflyway 进阶教程:KCP 协议复活被墙IP 决定试一试.在vultr上临时搭建了测试环境,可能 ...
- python模拟大数据登陆
#针对tableu 撰写的大数据框架 #tesseract 识别简单的验证码 不多说 直接上代码 # coding:utf-8 from selenium import webdriver from ...
- 7.2.5 多层嵌套的if语句
7.2.5 多层嵌套的if语句 在编写程序的代码之前要先规划好.首先,要总体设计一下程序. 为方便起见,程序应该使用一个连续的循环让用户能连续输入待测试的 数.这样,测试一个新的数字不必每次都要重新运 ...
- IDEA注册码分享
IntelliJ IDEA IDEA 2018 激活注册码分享鼠标连续 三下左键点击 选中,再Ctrl+C 即可复制. CSDN在末尾会带上博客的说明,请删除后,复制到 IDEA中激活. 注册码激活: ...
- 神经网络常用的Numpy功能笔记
数组初始化 x=np.array([[1,2]]) x=np.zeros((2,3)) 生成随机数 w=np.random.randn(2,3) PIL image转换成array img = np. ...
- [STM32F103]外部中断
① 初始化IO口为输入. GPIO_Init(); ② 开启IO口复用时钟. RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); ③ 设置IO口与中 ...
- int x ; x+1<x;公式成立
直接上代码: Console.WriteLine("int取值范围 -2147483648-2147483647");int x = 2147483647;// Console.W ...
- jquery中文档处理的总结
jQuery文档处理总结 1.返回值:append(content|fn) $("p").append("<b>Hello</b>"); ...
- [UnityShader效果]01.Mask
参考链接: https://blog.csdn.net/akof1314/article/details/50428200 1.Mask.shader // Unity built-in shader ...