# 1640. Check Array Formation Through Concatenation ###### tags: `Leetcode` `Easy` `HashMap` Link: https://leetcode.com/problems/check-array-formation-through-concatenation/ ## 思路 $O(N)$ $O(N)$ 只要查看每个subarray里面的每个元素是不是满足顺序就行 首先把arr里面的数字和index存在hashmap里,首先锁定subarray里面第一个元素的index,然后看之后每个元素对应的index对不对 要注意有一些元素不在arr里面的状况 ## Code ```java= class Solution { public boolean canFormArray(int[] arr, int[][] pieces) { Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for(int i = 0;i < arr.length;i++){ map.put(arr[i], i); } for(int[] piece:pieces){ if(!map.containsKey(piece[0])) return false; if(piece.length>=2){ int start = map.get(piece[0]); for(int i = 1; i < piece.length;i++){ if(!map.containsKey(piece[i]) || start+i!=map.get(piece[i])){ return false; } } } } return true; } } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up