Skip to content

Commit

Permalink
1640. 能否连接形成数组
Browse files Browse the repository at this point in the history
  • Loading branch information
BGMer7 committed Mar 22, 2023
1 parent 51d4e1a commit 0ffd017
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/com/gatsby/_1640CheckArrayFormationThroughConcatenation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.gatsby;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @classname _1640CheckArrayFormationThroughConcatenation
* @description:
* @author: gatsby
* @create: 2022/9/22
**/
public class _1640CheckArrayFormationThroughConcatenation {
public boolean canFormArray(int[] arr, int[][] pieces) {
Map<Integer, int[]> map = new HashMap<>();

for (int[] piece : pieces) {
map.put(piece[0], piece);
}

for (int i = 0; i < arr.length; ) {
if (!map.containsKey(arr[i])) return false;
int[] array = map.get(arr[i]);
for (int j = 0; j < array.length; ++j, ++i) {
if (arr[i] != array[j]) return false;
}
}
return true;
}
}

0 comments on commit 0ffd017

Please sign in to comment.