前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Leetcode-Easy 985. Sum of Even Numbers After Queries

Leetcode-Easy 985. Sum of Even Numbers After Queries

作者头像
致Great
发布2019-03-06 09:54:47
3370
发布2019-03-06 09:54:47
举报
文章被收录于专栏:程序生活程序生活

题目描述

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.

思路

新建一个列表,保留更新后的偶数值,将奇数值设为0,然后求和

代码实现

class Solution:
    def sumEvenAfterQueries(self, A: 'List[int]', queries: 'List[List[int]]') -> 'List[int]':
        new_A=[None]*len(A)
        even_index=[a if a%2==0 else 0 for a in A]
        for i in range(len(A)):
            val = queries[i][0]
            index = queries[i][1]
            A[index]+=val
            if A[index]%2==0:
                even_index[index]=A[index]
            else:
                even_index[index]=0
            new_A[i]=sum(even_index)
        return new_A
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.02.17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述
  • 思路
  • 代码实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档