【LEETCODE】47、985. Sum of Even Numbers After Queries
package y2019.Algorithm.array; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: SumEvenAfterQueries
* @Author: xiaof
* @Description: 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][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.
*
* 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.
*
* @Date: 2019/7/4 16:35
* @Version: 1.0
*/
public class SumEvenAfterQueries { public int[] solution(int[] A, int[][] queries) { int[] result = new int[queries.length];
for(int i = 0; i < queries.length; ++i) {
//计算操作
int[] temp = queries[i];
A[temp[1]] = A[temp[1]] + temp[0];
//计算相应位置的数据
int tempResult = 0;
for(int j = 0; j < A.length; ++j) {
if((A[j] & 1) == 0) {
tempResult += A[j];
}
}
result[i] = tempResult;
} return result;
} public static void main(String args[]) { int[] A = {1,2,3,4};
int[][] queries = {{1,0},{-3,1},{-4,0},{2,3}}; SumEvenAfterQueries fuc = new SumEvenAfterQueries();
System.out.println(fuc.solution(A, queries));
}
}
【LEETCODE】47、985. Sum of Even Numbers After Queries的更多相关文章
- 【LeetCode】1、Two Sum
题目等级:Easy 题目描述: Given an array of integers, return indices of the two numbers such that they add u ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
- 【LeetCode】18、四数之和
题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...
- 【LeetCode】9、Palindrome Number(回文数)
题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【LeetCode】714、买卖股票的最佳时机含手续费
Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...
- 【Leetcode_easy】985. Sum of Even Numbers After Queries
problem 985. Sum of Even Numbers After Queries class Solution { public: vector<int> sumEvenAft ...
- 【LeetCode】47. Permutations II 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...
- 【LeetCode】4、Median of Two Sorted Arrays
题目等级:Hard 题目描述: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find t ...
随机推荐
- Xamarin安装及调试
Xamarin介绍 Xamarin是一个跨平台的开发框架(工具集),创始于2011年,旨在使移动开发变得难以置信地迅捷和简单,它是跨平台的,它允许开发人员有效创建可跨 iOS.Android.Wind ...
- ipcm
用来删除一个或更多的消息队ipcm列.信号量集或者共享内存标识
- 2017.10.4 国庆清北 D4T2 正方形
题目描述 在一个10000*10000的二维平面上,有n颗糖果. LYK喜欢吃糖果!并且它给自己立了规定,一定要吃其中的至少C颗糖果! 事与愿违,LYK只被允许圈出一个正方形,它只能吃在正方形里面的糖 ...
- 2、kafka集群搭建
以三台为例,先安装一台,然后分发: 一.准备 1.下载 http://kafka.apache.org kafka_2.11-2.0.1.tgz 前面的数字2.11是scala的版本,2.0.1是ka ...
- mysql 数据插入insert
mysql> select * from user; +------+----------+-----------+ | id | name | address | +------+------ ...
- Linux最大线程数限制
开始以为是内存不足导致无法创建线程,把jvm的-Xms,-Xmx的2个参数都加大一倍:-Xms2048m -Xmx2048m.把-Xss参数调小,还是启动失败.应该是系统方面的限制了,这台机器上搞了1 ...
- 微信小程序之确认框实现
效果图如下: 核心代码如下: delType:function(e){ var typeId = e.currentTarget.dataset['id']; console.log("de ...
- [转]OpenGL图形渲染管线、VBO、VAO、EBO概念及用例
直接给出原文链接吧 1.OpenGL图形渲染管线.VBO.VAO.EBO概念及用例 2.OpenGL中glVertex.显示列表(glCallList).顶点数组(Vertex array).VBO及 ...
- deepnude | 福利
程序好下载github有,但是没有lib,就是没有训练好的model. 以下是搜到的win平台程序的下载链接: magnet:?xt=urn:btih:7BE4EB8D640742D2FFEBD649 ...
- spring源码的设计模式
转:https://blog.csdn.net/huyang0304/article/details/82928900 接下来我们只介绍在Spring中常用的设计模式. 1.1.简单工厂模式(Fact ...