133. Clone Map

133. Clone Map

Title description

You undirected communication reference a node diagram, you return to this figure deep copy (clone).

Each node in the graph contains its value val( int) and a list of its neighbors ( list[Node]).

class Node {
    
    
    public int val;
    public List<Node> neighbors;
}

Test case format:

For simplicity, the value of each node is the same as its index. For example, the value of the first node is 1 ( val = 1), the value of the second node is 2 ( val = 2), and so on. The graph is represented by the adjacency list in the test case.

The adjacency list is a collection of unordered lists used to represent finite graphs. Each list describes the set of neighbors of the nodes in the graph.

The given node will always be the first node in the graph (value 1). You must be a copy of the given node as a reference to returns clones FIG.

Example 1:


输入:adjList = [[2,4],[1,3],[2,4],[1,3]]
输出:[[2,4],[1,3],[2,4],[1,3]]
解释:
图中有 4 个节点。
节点 1 的值是 1,它有两个邻居:节点 24 。
节点 2 的值是 2,它有两个邻居:节点 13 。
节点 3 的值是 3,它有两个邻居:节点 24 。
节点 4 的值是 4,它有两个邻居:节点 13

Example 2:


输入:adjList = [[]]
输出:[[]]
解释:输入包含一个空列表。该图仅仅只有一个值为 1 的节点,它没有任何邻居。

Example 3:

输入:adjList = []
输出:[]
解释:这个图是空的,它不含任何节点。

Example 4:


输入:adjList = [[2],[1]]
输出:[[2],[1]]

prompt:

  1. The number of nodes does not exceed 100.
  2. Each node values Node.valare unique 1 <= Node.val <= 100.
  3. An undirected graph is a simple graph, which means that there are no repeated edges and no self-loops in the graph.
  4. Since the graph is undirected, if node p is a neighbor of node q, then node q must also be a neighbor of node p.
  5. The graph is a connected graph, and you can access all nodes from a given node.

answer:

This question deep copy is a copy of the original map, but the need to open up a new space to save.

Can DFS of the original search, hash table holding picture corresponding new nodes and node open space , will be connected to the recursive procedure in FIG.

Time complexity: O (n) O(n)O ( n )

Extra space complexity: O (n) O(n)O ( n )

/*
// 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:
    unordered_map<int, Node*> hash;
    void dfs( Node* node ) {
    
    
        hash[node->val] = new Node( node->val );
        for( const auto& it : node->neighbors ) {
    
    
            if ( hash.find( it->val ) == hash.end() ) 
                dfs( it );
            hash[node->val]->neighbors.emplace_back( hash[it->val] );
        }
    }
    Node* cloneGraph(Node* node) {
    
    
        if( !node ) return nullptr;
        dfs( node );
        return hash[node->val];
    }
};
/*
时间:4ms,击败:95.41%
内存:8.7MB,击败:78.18%
*/

Guess you like

Origin blog.csdn.net/MIC10086/article/details/114001794