-
发表于 2024.05.31
-
挺简单的数组题目,首先累加一下数组中的偶数和,然后对每个查询进行偶数和的更新即可:如果查询前的数为偶数,那么需要减去原数值,如果查询后的数为偶数,那么需要加上新数值。
class Solution: def sumEvenAfterQueries(self, nums: List[int], queries: List[List[int]]) -> List[int]: cur_even_sum = sum(num for num in nums if not num % 2) ans = [] for diff, index in queries: old = nums[index] new = old + diff if not old % 2: cur_even_sum -= old if not new % 2: cur_even_sum += new nums[index] = new ans.append(cur_even_sum) return ans
- LC 题目链接
-