mirror of
https://github.com/Kiritow/OJ-Problems-Source.git
synced 2024-03-22 13:11:29 +08:00
Create 1640.cpp
This commit is contained in:
parent
915867e8f7
commit
c88ede9274
37
LeetCode/1640.cpp
Normal file
37
LeetCode/1640.cpp
Normal file
|
@ -0,0 +1,37 @@
|
|||
class Solution {
|
||||
public:
|
||||
bool stepSearch(vector<int>& arr, vector<vector<int>>& pieces, vector<bool>& visited, int startIndex) {
|
||||
for (int i = 0; i < pieces.size(); i++)
|
||||
{
|
||||
if (visited[i]) continue;
|
||||
if (pieces[i][0] != arr[startIndex]) continue;
|
||||
if (startIndex + pieces[i].size() > arr.size()) continue;
|
||||
|
||||
// check all elements
|
||||
int j = 1;
|
||||
for (; j < pieces[i].size(); j++) {
|
||||
if (arr[startIndex + j] != pieces[i][j]) break;
|
||||
}
|
||||
if (j != pieces[i].size()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (arr.size() == startIndex + pieces[i].size()) return true;
|
||||
|
||||
// step down
|
||||
visited[i] = true;
|
||||
bool ret = stepSearch(arr, pieces, visited, startIndex + pieces[i].size());
|
||||
visited[i] = false;
|
||||
|
||||
if (ret) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool canFormArray(vector<int>& arr, vector<vector<int>>& pieces) {
|
||||
vector<bool> visited(pieces.size(), false);
|
||||
|
||||
return stepSearch(arr, pieces, visited, 0);
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user