#力扣 LeetCode985. 查詢後的偶數和 @FDDLC
阿新 • • 發佈:2021-02-17
技術標籤:演算法&資料結構
題目描述:
https://leetcode-cn.com/problems/sum-of-even-numbers-after-queries/
Java程式碼:
class Solution { public int[] sumEvenAfterQueries(int[] A, int[][] queries) { int[] ans=new int[queries.length]; int evenSum=0,cnt=0; for(int a:A)if((a&1)==0)evenSum+=a; for(int[] vi:queries){ if((A[vi[1]]&1)==0)evenSum-=A[vi[1]]; A[vi[1]]+=vi[0]; if((A[vi[1]]&1)==0)evenSum+=A[vi[1]]; ans[cnt++]=evenSum; } return ans; } }