topic
You are given an array arr of integers, each integer in the array is different from each other. There is also an array pieces of arrays of integers, the integers in which are also different from each other. Please
concatenate the arrays in pieces in any order to form arr. However, reordering of the integers in each array pieces[i] is not allowed.
Returns true if the arrays in pieces can be concatenated to form arr; otherwise, returns false.
Example 1:
Input: arr = [15,88], pieces = [[88],[15]]
Output: true
Explanation: Connect [15] and [88] in sequence
Example 2:
Input: arr = [49,18,16], pieces = [[16,18,49]]
Output: false
Explanation: Even if the numbers match, pieces[0] cannot be rearranged
Example 3:
Input: arr = [91,4,64,78], pieces = [[78],[4,64],[91]]
Output: true
Explanation: Connect [91], [4,64] and [78 in sequence ]
hint:
1 <= pieces.length <= arr.length <= 100
sum(pieces[i].length) == arr.length
1 <= pieces[i].length <= arr.length
1 <= arr[i], pieces[i][j] <= 100
Integers in arr are different from each other
Integers in pieces are different from each other (that is, if you flatten pieces into a 1D array, all integers in the array are different from each other)
answer
class Solution {
public:
bool canFormArray(vector<int>& arr, vector<vector<int>>& pieces) {
int len1 = arr.size();
int len2 = pieces.size();
for(int i=0;i<len2;i++)
{
auto bound = find(arr.begin(),arr.end(),pieces[i][0]);
if(bound == arr.end())
return false;
//只要每个pieces符合原数组的顺序,就能重组
for(auto it:pieces[i])
{
if(it != *bound)
return false;
bound++;
}
}
return true;
}
};