Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
sequence-reconstruction.cpp 1.09 KB
一键复制 编辑 原始数据 按行查看 历史
kamyu 提交于 2016-11-06 00:07 +08:00 . Create sequence-reconstruction.cpp
// Time: O(n * s), n is the size of org, s is the size of seqs
// Space: O(n)
class Solution {
public:
bool sequenceReconstruction(vector<int>& org, vector<vector<int>>& seqs) {
if (seqs.empty()) {
return false;
}
vector<int> pos(org.size() + 1);
for (int i = 0; i < org.size(); ++i) {
pos[org[i]] = i;
}
vector<bool> is_matched(org.size() + 1);
int cnt_to_match = org.size() - 1;
for (const auto& seq : seqs) {
for (int i = 0; i < seq.size(); ++i) {
if (seq[i] <= 0 || seq[i] > org.size()) {
return false;
}
if (i == 0) {
continue;
}
if (pos[seq[i - 1]] >= pos[seq[i]]) {
return false;
}
if (is_matched[seq[i - 1]] == false && pos[seq[i - 1]] + 1 == pos[seq[i]]) {
is_matched[seq[i - 1]] = true;
--cnt_to_match;
}
}
}
return cnt_to_match == 0;
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助