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

Commit0298b44

Browse files
authored
Update Find the Prefix Common Array of Two Arrays.java
1 parent9d40369 commit0298b44

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

‎Medium/Find the Prefix Common Array of Two Arrays.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
classSolution {
22
publicint[]findThePrefixCommonArray(int[]A,int[]B) {
3-
Map<Integer,Integer>map =newHashMap<>();
43
intcount =0;
5-
int[]result =newint[A.length];
6-
for (inti =0;i <A.length;i++) {
7-
map.put(A[i],map.getOrDefault(A[i],0) +1);
8-
map.put(B[i],map.getOrDefault(B[i],0) +1);
9-
count +=map.get(A[i]) ==2 ?1 :0;
10-
if (A[i] !=B[i]) {
11-
count +=map.get(B[i]) ==2 ?1 :0;
4+
intn =A.length;
5+
int[]result =newint[n];
6+
int[]frequency =newint[n +1];
7+
for (inti =0;i <n;i++) {
8+
frequency[A[i]]++;
9+
if (frequency[A[i]] ==2) {
10+
count++;
11+
}
12+
frequency[B[i]]++;
13+
if (frequency[B[i]] ==2) {
14+
count++;
1215
}
1316
result[i] =count;
1417
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp