【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 ...
随机推荐
- vscode vue文件格式化没效果
在vscode 中 格式化vue文件没效果 解决办法: 点击头部文件 >首选项>设置 在右侧加入这两句 "vetur.format.defaultFormatter.js&q ...
- Cogs 732. [网络流24题] 试题库(二分图)
[网络流24题] 试题库 ★★ 输入文件:testlib.in 输出文件:testlib.out 评测插件 时间限制:1 s 内存限制:128 MB «问题描述: 假设一个试题库中有n道试题.每道试题 ...
- mysql 获取字符串的长度
mysql> select * from test; +----+------------+-------+-----------+ | id | name | score | subject ...
- [HAOI2018]染色(NTT)
前置芝士 可重集排列 NTT 前置定义 \[\begin{aligned}\\ f_i=C_m^i\cdot \frac{n!}{(S!)^i(n-iS)!}\cdot (m-i)^{n-iS}\\ ...
- MongoDB 数据库备份还原
数据库备份 在 Mongodb 中我们使用 mongodump 命令来备份 MongoDB 数据.该命令可以导出所有数据 到指定目录中. mongodump 命令可以通过参数指定导出的数据量级转存的服 ...
- fluent meshing建立周期性网格
原视频下载地址:https://pan.baidu.com/s/1pKUXKgz 密码: 6pwh
- mongodb启动命令与端口设置
一.mongodb安装和配置 1.创建tools目录,用于存放安装包 cd /usr/local mkdir -p tools cd tools 2.下载mongodb包(其它版本请自行下载) wge ...
- HBase 介绍
HBase的列族式存储 列族式存储的概念 HBase Table的组成 Table = RowKey + Family + Column + Timestamp + Value 数据存储模式 (Row ...
- Python面向对象编程核心思想
原文地址https://blog.csdn.net/weixin_42134789/article/details/80194788 https://blog.csdn.net/happyjxt/ar ...
- 帆软 联合 创始人 数据可视化 中国 发展 FineReport FineBI
丧心病狂!帆软公司的成立竟源于一个被初恋抛弃的程序员 - 大数据-炼数成金-Dataguru专业数据分析社区http://dataguru.cn/article-7500-1.html 帆软联合创始人 ...