ランタイムエラー:タイプ 'std :: vector <int、std :: allocator <int >>'(stl_vector.h)のnullポインタへの参照バインディング
Sep 08 2020
これはリートコード210
です。0からn-1までのラベルが付いた合計nのコースを受講する必要があります。
一部のコースには前提条件がある場合があります。たとえば、prepreditions [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;
}
};
回答
1 Emma Sep 08 2020 at 16:52
ここには小さなタイプミスしかありません:
return vecttoreturn myvect。別の小さなバグはここにあります:
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;
}
};