English 中文(简体)
为什么使用实例变量比莱特居地的当地变量要多记忆? [闭门]
原标题:Why using instance variables takes more memory compare to local variables in LeetCode? [closed]
Closed. This question needs debugging details. It is not currently accepting answers.

我正试图利用DFS处理LeetCode问题。 由此可见,使用实例变量比当地变量多出空间。

我的守则是:

http://www.ohchr.org。

class Solution {
public:
    
    void dfs(int node, int parent, int& counter, vector<int>& low, vector<int>& id_to_rank, vector<vector<int>>& edges, vector<vector<int>>& ans){

        id_to_rank[node] = counter;
        low[node] = counter;
        counter++;

        for (const auto& e : edges[node]){
            if (parent == e) continue;

            if (id_to_rank[e] == -1){
                dfs(e, node, counter, low, id_to_rank, edges, ans);
            
                low[node] = min(low[node], low[e]);

                if (low[e] > id_to_rank[node]){
                    ans.push_back({e, node});
                }
            }else{
                low[node] = min(low[node], low[e]);
            }

        }

        return;
    }

    vector<vector<int>> criticalConnections(int n, vector<vector<int>>& connections) {
        vector<vector<int>> edges(n);
        vector<int> id_to_rank (n, -1);
        vector<int> low(n);

        vector<vector<int>> ans;
        int counter = 0;

        for (const auto& c : connections){
            edges[c[0]].push_back(c[1]);
            edges[c[1]].push_back(c[0]);

        }

        dfs(0, -1, counter, low, id_to_rank, edges, ans);

        return ans;
    }
};

<<>Instance

class Solution {
public:
    vector<vector<int>> ans;
    int counter = 0;

    void dfs(int node, int parent, vector<int>& low, vector<int>& id_to_rank, vector<vector<int>>& edges){

        id_to_rank[node] = counter;
        low[node] = counter;
        counter++;

        for (const auto& e : edges[node]){
            if (parent == e) continue;

            if (id_to_rank[e] == -1){
                dfs(e, node, low, id_to_rank, edges);
            
                low[node] = min(low[node], low[e]);

                if (low[e] > id_to_rank[node]){
                    ans.push_back({e, node});
                }
            }else{
                low[node] = min(low[node], low[e]);
            }

        }

        return;
    }

    vector<vector<int>> criticalConnections(int n, vector<vector<int>>& connections) {
        vector<vector<int>> edges(n);
        vector<int> id_to_rank (n, -1);
        vector<int> low(n);

        for (const auto& c : connections){
            edges[c[0]].push_back(c[1]);
            edges[c[1]].push_back(c[0]);

        }

        dfs(0, -1, low, id_to_rank, edges);

        return ans;
    }
};

我多次重新提出解决办法,差额不变(约10%的记忆)。 我的猜测是,LeetCode在每一个试验场都使用同样的物体,因此是不同的。 但即便如此,我仍然不知道究竟发生了什么。

为什么使用实例变量比莱特居地的当地变量要多记忆?

(这是我第一次提出疑问,如果存在我不了解的任何规则,我就知道)

问题回答

<代码>return ans 正在返回COPY of the member,ans, 接获时,请将对该物体的记忆增加一倍,

Solution sol; // has the vector
auto answer = sol.criticalConnections(...) // a copy is made

you can instead move the member out.

return std::move(ans)


// the vector is moved into answer
auto answer = sol.criticalConnections(...) 
// after this line sol.ans is empty

as for the local variable version since the variable is not going to be used anywhere anymore, the compiler can do NRVO (copy elision) on it and not create the copy, and even if NRVO failed it should at least move it on its own.

unfortunately competitive coding sites only teach you competitive coding, they don t teach you coding, and they don t teach you C++, you are only going to learn c++ from a proper c++ textbook





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签