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的更多相关文章

  1. 【LeetCode】1、Two Sum

    题目等级:Easy 题目描述:   Given an array of integers, return indices of the two numbers such that they add u ...

  2. 【LeetCode】15、三数之和为0

    题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...

  3. 【LeetCode】18、四数之和

    题目等级:4Sum(Medium) 题目描述: Given an array nums of n integers and an integer target, are there elements ...

  4. 【LeetCode】9、Palindrome Number(回文数)

    题目等级:Easy 题目描述: Determine whether an integer is a palindrome. An integer is a palindrome when it rea ...

  5. 【LeetCode】 454、四数之和 II

    题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...

  6. 【LeetCode】714、买卖股票的最佳时机含手续费

    Best Time to Buy and Sell Stock with Transaction Fee 题目等级:Medium 题目描述: Your are given an array of in ...

  7. 【Leetcode_easy】985. Sum of Even Numbers After Queries

    problem 985. Sum of Even Numbers After Queries class Solution { public: vector<int> sumEvenAft ...

  8. 【LeetCode】47. Permutations II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:递归 方法二:回溯法 日期 题目地址:htt ...

  9. 【LeetCode】4、Median of Two Sorted Arrays

    题目等级:Hard 题目描述:   There are two sorted arrays nums1 and nums2 of size m and n respectively.   Find t ...

随机推荐

  1. SPA 首屏加载性能优化之 vue-cli3 拆包配置

    前言 现在已经是vue-cli3.x    webpack4.x 的时代了,但是网上很多拆包配置还是一些比较低版本的. 本文主要是分享自己的拆包踩坑经验. 主要是用了webpack4 的 splitC ...

  2. mysql distinct()函数 去重

    mysql> select * from table1; +----------+------------+-----+---------------------+ | name_new | t ...

  3. css文字两行或者几行显示省略号

    做这个省略的问题,突然发现显示省略号是有中英文区分的 我做两行的时候用的是以下代码,在是中文的情况下是么得问题,到了英文下发现不起作用了 width: 250px; overflow: hidden; ...

  4. 用Visual Studio编写UDF的一点小技巧(二)

  5. hdoj - 1506 直方图中最大的矩形

    Problem Description A histogram is a polygon composed of a sequence of rectangles aligned at a commo ...

  6. 范仁义html+css课程---6、表格

    范仁义html+css课程---6.表格 一.总结 一句话总结: 表格中最常用的元素就是table.tr.td.th,还有语义化的thead.tbody.tfoot标签 1.表格构成三个基本要素? t ...

  7. Spark2.x(六十):在Structured Streaming流处理中是如何查找kafka的DataSourceProvider?

    本章节根据源代码分析Spark Structured Streaming(Spark2.4)在进行DataSourceProvider查找的流程,首先,我们看下读取流数据源kafka的代码: Spar ...

  8. Gitlab修改用户密码

    sudo gitlab-rails console production > user=User.where(name: "root").first > user.pa ...

  9. linux 运行时限制CPU核数、内存、读写速度

    systemd-run --scope -p MemoryLimit=4G \ -p "BlockIOReadBandwidth=/dev/sdb1 150M" \ -p &quo ...

  10. Python推荐一整套开发工具

    原文:https://sourcery.ai/blog/python-best-practices/ 在开始一个新的Python项目时,很容易不做规划直接进入编码环节.花费少量时间,用最好的工具设置项 ...