Skip to content

Commit 0ffd017

Browse files
committed
1640. 能否连接形成数组
1 parent 51d4e1a commit 0ffd017

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.gatsby;
2+
3+
import java.util.HashMap;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
/**
8+
* @classname _1640CheckArrayFormationThroughConcatenation
9+
* @description:
10+
* @author: gatsby
11+
* @create: 2022/9/22
12+
**/
13+
public class _1640CheckArrayFormationThroughConcatenation {
14+
public boolean canFormArray(int[] arr, int[][] pieces) {
15+
Map<Integer, int[]> map = new HashMap<>();
16+
17+
for (int[] piece : pieces) {
18+
map.put(piece[0], piece);
19+
}
20+
21+
for (int i = 0; i < arr.length; ) {
22+
if (!map.containsKey(arr[i])) return false;
23+
int[] array = map.get(arr[i]);
24+
for (int j = 0; j < array.length; ++j, ++i) {
25+
if (arr[i] != array[j]) return false;
26+
}
27+
}
28+
return true;
29+
}
30+
}

0 commit comments

Comments
 (0)