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. cjss 像编写css 一样开发web应用

    cjss 提供了使用类似css 的方式编写web 应用 cjss 包含的阶段 data prepare body element 几点说明 并不是所以阶段必须使用,但是每个级别只能存在一个script ...

  2. vue-cli3 ios10白屏问题解决思路

    在出现了这个问题之后先不要盲目的去瞎试,根据网上的方法试了个遍也没解决问题 先看报的是什么错,再针对的解决问题 首先出现的报错是 SyntaxError: Unexpected token '*'  ...

  3. 01-复杂度2 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  4. App数据指标

    App数据指标 1 App数据指标 2 参考资料 超详细的APP数据指标体系分析

  5. mysql 选择所有同学名字

    mysql> select * from test; +----+----------+-------+-----------+ | id | name | score | subject | ...

  6. MySQL8.0报错Can't connect to MySQL server on 'localhost' (10061)的解决办法

    MySQL8.0报错Can't connect to MySQL server on 'localhost' (10061)的解决办法 事情的起因     今天课堂上要展示小组项目,需要用一个软件叫W ...

  7. 作业——09 安装关系型数据库MySQL 安装大数据处理框架Hadoop

    作业的要求来自于:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE2/homework/3161 简述Hadoop平台的起源.发展历史与应用现状. 起源: 2 ...

  8. php常用命令

    --------------------------------------------------------------- 重启phpservice php-fpm restart ------- ...

  9. ISO/IEC 9899:2011 前言

    前言 1.ISO(国际标准组织)与IEC(国际电工技术委员会)为全世界标准形成了专门的系统.作为ISO或IEC成员的国家机构,通过由各自组织所建立的技术委员会来加入国际标准的开发,以处理特定领域的技术 ...

  10. Hive Authorization

    https://cwiki.apache.org/confluence/display/Hive/LanguageManual+Authorization https://www.cloudera.c ...