Leecode 133. 克隆图 DFS/BFS

news/2024/7/20 22:34:18 标签: 深度优先, 宽度优先, leetcode, c++, 算法

原题链接:Leecode 133. 克隆图
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
官解:Leecode 133. 克隆图

DFS

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> neighbors;
    Node() {
        val = 0;
        neighbors = vector<Node*>();
    }
    Node(int _val) {
        val = _val;
        neighbors = vector<Node*>();
    }
    Node(int _val, vector<Node*> _neighbors) {
        val = _val;
        neighbors = _neighbors;
    }
};
*/

class Solution {
public:
    map<Node*,Node*> m;
    Node* cloneGraph(Node* node) {
        if(node==NULL) return node;
        if(m.find(node)!=m.end()) return m[node];
        Node* root=new Node(node->val);
        m[node]=root;
        for(auto neighbor : node->neighbors)
        {
            root->neighbors.push_back(cloneGraph(neighbor));
        }
        return root;
    }
};

BFS

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> neighbors;
    Node() {
        val = 0;
        neighbors = vector<Node*>();
    }
    Node(int _val) {
        val = _val;
        neighbors = vector<Node*>();
    }
    Node(int _val, vector<Node*> _neighbors) {
        val = _val;
        neighbors = _neighbors;
    }
};
*/

class Solution {
public:
    map<Node*,Node*> m;
    Node* cloneGraph(Node* node) {
        if(node==NULL) return node;
        queue<Node* > q;
        m[node]=new Node(node->val);
        q.push(node);
        while(!q.empty())
        {
            auto tmp=q.front(); q.pop();
            for(auto neighbor : tmp->neighbors)
            {
                if(m.find(neighbor)==m.end())
                {
                    m[neighbor]=new Node(neighbor->val);
                    q.push(neighbor);
                }
                m[tmp]->neighbors.push_back(m[neighbor]);
            }
        }
        return m[node];
    }
};



http://www.niftyadmin.cn/n/1264726.html

相关文章

df_1.columns

Python版本&#xff1a;Python 3.6 pandas.read_csv() 报错 OSError: Initializing from file failed&#xff0c;一般由两种情况引起&#xff1a;一种是函数参数为路径而非文件名称&#xff0c;另一种是函数参数带有中文&#xff08;包括路径里边有中文&#xff09;。 # -*- …

Leecode 1034. 边界着色 DFS/BFS

原题链接&#xff1a;Leecode 1034. 边界着色 这题题目不太好理解 DFS class Solution { public:int x[4]{-1,1,0,0};int y[4]{0,0,-1,1};vector<vector<int>> vis;bool dfs(vector<vector<int>>& grid,int i,int j,int col,int color){int mg…

在notebook中使用plt绘图共有三种模式

在notebook中使用plt绘图共有三种模式&#xff1a; %matplotlib inline&#xff1a;这是默认的模式&#xff0c;输出的图片是静态的%matplotlib auto&#xff1a;在这个模式下会弹出一个单独 的绘图窗口&#xff0c;和在pycharm中一样%matplotlib notebook&#xff1a;在这个模…

更换anaconda的镜像频道

1. 添加“清华镜像”渠道&#xff0c; 在Anaconda Prompt中执行&#xff1a; conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/ conda…

Leecode 417. 太平洋大西洋水流问题 DFS

原题链接&#xff1a;Leecode 417. 太平洋大西洋水流问题 从太平洋和大西洋各自遍历&#xff0c;重合的点即为答案。 class Solution { public:vector<vector<int>> P,A,res;void dfs(vector<vector<int>>& heights,vector<vector<int&g…

复杂网络中louvain算法实现时报错AttributeError: module ‘community‘ has no attribute ‘best_partition‘

导入包的方式有点奇怪&#xff0c;用的不是包名“python-louvain”而是“community”&#xff0c; import community as community_louvain 在jupyter中运行“partition community_louvain.best_partition(G) #进行图划分”的时候出现以下错误&#xff1a; AttributeError:…

ImportError: cannot import name ‘joblib‘

原因&#xff1a; 安装的Scikit-learn版本太高&#xff0c;我安装的版本是0.23.1 解决方法&#xff1a; 需要将Scikit-learn版本降到0.21以下 pip uninstall joblib scikit-learn sklearn pip install Scikit-learn0.20.4 或者直接安装joblib&#xff1a; pip install j…

Leecode 1732. 找到最高海拔 前缀和

原题链接&#xff1a;Leecode 1732. 找到最高海拔 class Solution { public:int largestAltitude(vector<int>& gain) {int res0,tmp0;for(auto h: gain){tmph;resmax(res,tmp);}return res;} };