Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7e32798

Browse files
refactor 985
1 parentff9a99f commit7e32798

File tree

1 file changed

+18
-45
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+18
-45
lines changed
Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,25 @@
11
packagecom.fishercoder.solutions;
22

3-
/**
4-
* 985. Sum of Even Numbers After Queries
5-
*
6-
* We have an array A of integers, and an array queries of queries.
7-
* 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.
8-
* (Here, the given index = queries[i][1] is a 0-based index, and each query permanently modifies the array A.)
9-
* Return the answer to all queries. Your answer array should have answer[i] as the answer to the i-th query.
10-
*
11-
* Example 1:
12-
*
13-
* Input: A = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]]
14-
* Output: [8,6,2,4]
15-
* Explanation:
16-
* At the beginning, the array is [1,2,3,4].
17-
* After adding 1 to A[0], the array is [2,2,3,4], and the sum of even values is 2 + 2 + 4 = 8.
18-
* After adding -3 to A[1], the array is [2,-1,3,4], and the sum of even values is 2 + 4 = 6.
19-
* After adding -4 to A[0], the array is [-2,-1,3,4], and the sum of even values is -2 + 4 = 2.
20-
* After adding 2 to A[3], the array is [-2,-1,3,6], and the sum of even values is -2 + 6 = 4.
21-
*
22-
* Note:
23-
*
24-
* 1 <= A.length <= 10000
25-
* -10000 <= A[i] <= 10000
26-
* 1 <= queries.length <= 10000
27-
* -10000 <= queries[i][0] <= 10000
28-
* 0 <= queries[i][1] < A.length
29-
*/
303
publicclass_985 {
31-
publicstaticclassSolution1 {
32-
publicint[]sumEvenAfterQueries(int[]A,int[][]queries) {
33-
int[]result =newint[A.length];
34-
for (inti =0;i <A.length;i++) {
35-
intcol =queries[i][1];
36-
A[col] =A[col] +queries[i][0];
37-
result[i] =computeEvenSum(A);
38-
}
39-
returnresult;
40-
}
4+
publicstaticclassSolution1 {
5+
publicint[]sumEvenAfterQueries(int[]A,int[][]queries) {
6+
int[]result =newint[A.length];
7+
for (inti =0;i <A.length;i++) {
8+
intcol =queries[i][1];
9+
A[col] =A[col] +queries[i][0];
10+
result[i] =computeEvenSum(A);
11+
}
12+
returnresult;
13+
}
4114

42-
privateintcomputeEvenSum(int[]A) {
43-
intsum =0;
44-
for (intnum :A) {
45-
if (num %2 ==0) {
46-
sum +=num;
15+
privateintcomputeEvenSum(int[]A) {
16+
intsum =0;
17+
for (intnum :A) {
18+
if (num %2 ==0) {
19+
sum +=num;
20+
}
21+
}
22+
returnsum;
4723
}
48-
}
49-
returnsum;
5024
}
51-
}
5225
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp