【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 ...
随机推荐
- 钟长者P71解题报告
T1 [题目描述] 给你N个字符串,你每次可以选择其中一个字符串的一段前缀进行翻转,但是你必须保证这个前缀的长度是偶数.你可以进行无限次这样的操作,并且如果两个字符串变得相同的时候,你就可以把这两个字 ...
- USACO 奶牛抗议 Generic Cow Protests
USACO 奶牛抗议 Generic Cow Protests Description 约翰家的N头奶牛聚集在一起,排成一列,正在进行一项抗议活动.第i头奶牛的理智度 为Ai,Ai可能是负数.约翰希望 ...
- Kubernetes集群部署(yum部署)
环境准备 Kubernetes-Master:192.168.37.134 #yum install kubernetes-master etcd flannel -y Kubernetes-n ...
- PHP 之根据两个经纬度计算距离
一.函数代码 /** * @param $lng1 * @param $lat1 * @param $lng2 * @param $lat2 * @return float */ function g ...
- centos7最小化安装准备工作
1.配置网络 [root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eno16780032 HWADDR=00:0C:29:48:9 ...
- 牛顿插值法(c++)
X Y 0.40 0.41075 0.55 0.57815 0.65 0.69675 0.80 0.88811 0.90 1.02652 1.05 1.25382 #include using nam ...
- 【数值分析】Python实现Lagrange插值
一直想把这几个插值公式用代码实现一下,今天闲着没事,尝试尝试. 先从最简单的拉格朗日插值开始!关于拉格朗日插值公式的基础知识就不赘述,百度上一搜一大堆. 基本思路是首先从文件读入给出的样本点,根据输入 ...
- 再谈CAP
CAP定理设计者Eric Brewer作为Google基础设施副总裁在时隔二十年后重谈CAP定律. Eric Brewer目前正在推动Kubernetes和容器建设,在这篇采访中:Google sys ...
- DATEADD (Transact-SQL)
DATEADD (Transact-SQL) This function adds a specified number value (as a signed integer) to a specif ...
- mysql查询、子查询、连接查询
mysql查询.子查询.连接查询 一.mysql查询的五种子句 where子句(条件查询):按照“条件表达式”指定的条件进行查询. group by子句(分组):按照“属性名”指定的字段进行分组.gr ...