//1
class Solution {
int cnt=0;
public boolean canFormArray(int[] arr, int[][] pieces) {
return dfs(0,arr,pieces);
}
boolean dfs(int idx,int[] arr,int[][] pieces){
if(idx>arr.length)return false;
if(cnt==arr.length)return true;
for (int i = 0; i < pieces.length; i++) {
if(arr[idx]==pieces[i][0]){
for (int j = 0; j < pieces[i].length; j++) {
if(arr[idx+j]!=pieces[i][j])return false;
}
cnt+=pieces[i].length;
if(dfs(idx+pieces[i].length,arr,pieces))return true;
}
}
return false;
}
}
//2
class Solution {
int cnt=0;
public boolean canFormArray(int[] arr, int[][] pieces) {
int[] hash=new int[110];
for (int i = 0; i < pieces.length; i++) {
hash[pieces[i][0]]=i;
}
for (int i = 0; i < arr.length;) {
int[] tmp=pieces[hash[arr[i]]];
for (int j = 0; j < tmp.length; j++) {
if(arr[i+j]!=tmp[j])return false;
}
i+=tmp.length;
}
return true;
}
}
标签:arr,return,1640,idx,int,cnt,pieces,数组,leetcode
From: https://www.cnblogs.com/kotonoha/p/16719771.html