985. Sum of Even Numbers After Queries
We have an array
A
of integers, and an arrayqueries
of 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
answer
array 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 ...
随机推荐
- django orm 管理器 objects
给某张表的管理器重命名 class User(models.Model): name = models.CharField(max_length=100) people = models.Manage ...
- note 8 字符串
字符串String 一个字符的序列 使用成对的单引号或双引号括起来 或者三引号""" 和 ''' 表示块注释 字符串运算 长度 len()函数 first_name = ...
- 学习MeteoInfo二次开发教程(十二)
1.添加新的Form窗体: 在解决方案资源管理器中,右键MeteoInfoDemo,“添加”,“Windows 窗体” 2.新窗体中添加好layersLegend1和Layout之后,要把layers ...
- 移植QT库的问题:QT_INSTALL/include/QtCore/qatomic_arm.h:131: Error: no such instruction: `swpb %al,
解决办法:错误信息说明编译器未识别swpb汇编操作,指令集有问题.把配置命令改成: ./configure -embedded armv7 -prefix /home/thwijeth/Softwar ...
- 解决导出CSV后在EXCEL打开纯数字前面0丢失问题
select ip ,concat('="',accountname,'"')select ip ,concat('="',accountname,'"')
- Java学习笔记——鸵鸟学习记(二)
---恢复内容开始--- 4. 数组对象 4.1 数组的创建 a, 数组对象 在Java语言中,数组对象可以表示一组数字. int[] arr = new int[30];(new可以表示为创建 ...
- gentoo annie youku video
在gentoo 上面,如果需要下载 youku 的视频的话,可以使用 annie 这个软件来下载.annie 软件主页:https://github.com/iawia002/annie#instal ...
- ganglia之web界面介绍
转自:https://blog.csdn.net/lswnew/article/details/79175555 http://www.51niux.com/?id=83 第一篇文章讲述了gangli ...
- Mac 端配置 Lua 环境
一.设计目的 Lua 是一种轻量级的脚本语言,其目的是为了嵌入到程序中,从而为程序提供灵活的扩展和定制功能. 二.特性 轻量级:编译后仅仅 100 余K,可以很方便的嵌入到程序中. 可扩展:Lua 提 ...
- jquery选择器问题(找东西超级有用)
$("[class='slider-container theme-green']").css('width','100%');就这么一行代码,很简单,这样就很容易找到唯一元素