[英]Reference binding to null pointer of type 'std::vector<int, std::allocator<int>>' (stl_vector.h)
[英]runtime error: reference binding to null pointer of type 'std::vector<int, std::allocator<int> >' (stl_vector.h)
這是leetcode 210
總共有 n 門課程,您必須學習標記為 0 到 n - 1 的課程。
某些課程可能有先決條件,例如,如果先決條件[i] = [ai, bi] 這意味着您必須在課程 ai 之前參加課程 bi。
給定課程總數 numCourses 和先決條件對列表,返回完成所有課程所需的課程順序。
如果有很多有效答案,則返回其中任何一個。 如果不可能完成所有課程,則返回一個空數組。
輸入:numCourses = 4,先決條件 = [[1,0],[2,0],[3,1],[3,2]] 輸出:[0,2,1,3]
每當我在下面提交此代碼時,我都會收到runtime error: reference binding to null pointer of type 'std::vector<int, std::allocator<int> >' (stl_vector.h)
如果有人可以幫助我,我將不勝感激這個!
class Solution {
public:
vector<int> adj[2002];
bool vis[2002];
vector <int> myvect;
void dfs(int node)
{
if(!vis[node])
{
for(int i = 0; i < (int)adj[node].size(); i++)
{
if(!vis[adj[node][i]])
dfs(vis[adj[node][i]]);
}
myvect.push_back(node);
}
}
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites)
{
vector<int> adj[2002];
int n = prerequisites.size();
int m = prerequisites[0].size();
if(n == 0 && m == 0)
{
for(int i = 0; i < numCourses; i++){
myvect.push_back(i);
}
return myvect;
}
for(int i = 0; i < n; i++)
{
for(int j = 0; j < m; j++)
adj[i].push_back(j);
}
for(int i = 0; i < numCourses; i++)
dfs(i);
return myvect;
}
};
這里只有一個小錯誤: return vect
return myvect
。
另一個小錯誤在這里: int m = n ? prerequisites[0].size() : 0;
int m = n ? prerequisites[0].size() : 0;
修復這些錯誤后,該算法部分工作正常,但不適用於所有測試用例,不確定如何修復。 ( ^_^ )
class Solution {
public:
vector<int> adj[2002];
bool vis[2002];
vector <int> myvect;
void dfs(int node) {
if (!vis[node]) {
for (int i = 0; i < (int)adj[node].size(); i++) {
if (!vis[adj[node][i]]) {
dfs(vis[adj[node][i]]);
}
}
myvect.push_back(node);
}
}
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
vector<int> adj[2002];
int n = prerequisites.size();
int m = n ? prerequisites[0].size() : 0;
if (n == 0 && m == 0) {
for (int i = 0; i < numCourses; i++) {
myvect.push_back(i);
}
return myvect;
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
adj[i].push_back(j);
}
}
for (int i = 0; i < numCourses; i++) {
dfs(i);
}
return myvect;
}
};
// The following block might slightly improve the execution time;
// Can be removed;
static const auto __optimize__ = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
return 0;
}();
// Most of headers are already included;
// Can be removed;
#include <cstdint>
#include <vector>
using ValueType = std::uint_fast16_t;
using Graph = std::vector<std::vector<ValueType>>;
static const struct Solution {
static const std::vector<int> findOrder(
const int num_courses,
const std::vector<std::vector<int>>& prerequisites
) {
const Graph graph = buildGraph(num_courses, prerequisites);
std::vector<ValueType> indegrees = getIndegrees(graph);
std::vector<int> orders;
for (ValueType i = 0; i < num_courses; ++i) {
ValueType j = 0;
for (; j < num_courses; j++) {
if (!indegrees[j]) {
orders.emplace_back(j);
break;
}
}
if (j == num_courses) {
return {};
}
--indegrees[j];
for (const auto& edge : graph[j]) {
--indegrees[edge];
}
}
return orders;
}
private:
static const Graph buildGraph(
const int num_courses,
const std::vector<std::vector<int>>& prerequisites
) {
Graph graph(num_courses);
for (const auto& prerequisite : prerequisites) {
graph[prerequisite[1]].emplace_back(prerequisite[0]);
}
return graph;
}
static const std::vector<ValueType> getIndegrees(
const Graph& graph
) {
std::vector<ValueType> indegrees(std::size(graph), 0);
for (const auto& adj_edges : graph) {
for (const auto& edge : adj_edges) {
++indegrees[edge];
}
}
return indegrees;
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.