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 ...
随机推荐
- JavaScript 函数调用和this指针
函数调用和this指针 1. 全局环境的this指针 浏览器全局环境下this指向window对象 console.log(this); //Window {postMessage: ƒ, blur: ...
- 京东饭粒捡漏V1.15
20181105 更新 V1.151.部分BUG修复: 功能介绍1.京东商城专用,支持饭粒模式下单,自己获得京豆返利 2.捡漏模式:帮助用户监控抢购商品,有库存的时候进行抢单,主要是通过添加商品ID ...
- 根据导出的查询结果拼接字符串,生成sql语句并保存到txt文件中
import os os.chdir("C:/") path = os.getcwd() print(path) f = open("sql.csv") # p ...
- 玩了下opencv的aruco(python版)
简单的玩了下opencv里头的aruco,用的手机相机,手机装了个 ip摄像头,这样视频就可以传到电脑上了. 首先是标定,我没打印chessboard,直接在电脑屏幕上显示,拍了17张,大概如下: 又 ...
- 廖雪峰Java9正则表达式-1正则表达式入门-1正则表达式简介
1.使用代码来判断字符串 场景: 判断字符串是否是有效的电话号码:"010-12345678", "123ABC456" 判断字符串是否是有效的电子邮箱地址:& ...
- Java核心-多线程-并发控制器-Semaphore信号量
Semaphore是非常有用的一个多线程并发控制组件(Java还有CountDownLatch.CyclicBarrier.Exchanger多线程组件),它相当于是一个并发控制器,是用于管理信号量的 ...
- python之路——23
复习 1.类定义 函数--方法--动态属性 必须传self 变量--类属性--静态属性 __init__方法--初始化方法2.实例化 1.使用:对象 = 类() 2.实例和对象没有区别 3.对象调用方 ...
- 知识点:Mysql 索引原理完全手册(2)
知识点:Mysql 索引原理完全手册(1) 知识点:Mysql 索引原理完全手册(2) 知识点:Mysql 索引优化实战(3) 知识点:Mysql 数据库索引优化实战(4) 八. 联合索引与覆盖索引 ...
- maven dependency的版本冲突问题
在改造一个旧项目中,遇到各种问题. 旧项目有十多个模块,因为没有一个统一的父pom,它们对第三方的jar的版本没有统一. 虽然也存在公共的依赖模块,比如commons.util,但是,我们的模块中,有 ...
- PHP Yii2.0PHPexecl导出。
use phpoffice\phpexcel; public function actionExport(){ $objPHPExcel = new \phpexcel; $objPHPExcel - ...