给你无向 连通 图中一个节点的引用,请你返回该图的 深拷贝(克隆)。
图中的每个节点都包含它的值 val(int) 和其邻居的列表(list[Node])。
class Node {
public int val;
public List<Node> neighbors;
}
测试用例格式:
简单起见,每个节点的值都和它的索引相同。例如,第一个节点值为 1(val = 1),第二个节点值为 2(val = 2),以此类推。该图在测试用例中使用邻接列表表示。
邻接列表 是用于表示有限图的无序列表的集合。每个列表都描述了图中节点的邻居集。
给定节点将始终是图中的第一个节点(值为 1)。你必须将 给定节点的拷贝 作为对克隆图的引用返回。
示例 1:
输入:adjList = [[2,4],[1,3],[2,4],[1,3]]
输出:[[2,4],[1,3],[2,4],[1,3]]
解释:
图中有 4 个节点。
节点 1 的值是 1,它有两个邻居:节点 2 和 4 。
节点 2 的值是 2,它有两个邻居:节点 1 和 3 。
节点 3 的值是 3,它有两个邻居:节点 2 和 4 。
节点 4 的值是 4,它有两个邻居:节点 1 和 3 。
示例 2:
输入:adjList = [[]]
输出:[[]]
解释:输入包含一个空列表。该图仅仅只有一个值为 1 的节点,它没有任何邻居。
示例 3:
输入:adjList = []
输出:[]
解释:这个图是空的,它不含任何节点。
示例 4:
输入:adjList = [[2],[1]]
输出:[[2],[1]]
提示:
节点数不超过 100 。
每个节点值 Node.val 都是唯一的,1 <= Node.val <= 100。
无向图是一个简单图,这意味着图中没有重复的边,也没有自环。
由于图是无向的,如果节点 p 是节点 q 的邻居,那么节点 q 也必须是节点 p 的邻居。
图是连通图,你可以从给定节点访问到所有节点。
class Solution {
public:
Node* cloneGraph(Node* node) {//广度遍历
if(!node)return nullptr;
queue<Node*>q;
q.push(node);
unordered_map<Node*,Node*>m;//即表示映射又表示是否已遍历过
Node *newNode=new Node(node->val);
m[node]=newNode;
while(q.size()){
int qSize=q.size();
while(qSize--){
Node *qFront=q.front();
q.pop();
for(auto &n:qFront->neighbors){
if(m.count(n)==0){//
Node *temp=new Node(n->val);
m[n]=temp;
q.push(n);
}
m[qFront]->neighbors.push_back(m[n]);//
}
}
}
return newNode;
}
};
class Solution {
public:
Node* cloneGraph(Node* node) {//深度优先
if(!node)return nullptr;
stack<Node*>s;
s.push(node);
unordered_map<Node*,Node*>m;//即表示映射又表示是否已遍历过
Node *newNode=new Node(node->val);
m[node]=newNode;
while(s.size()){
Node *sTop=s.top();
s.pop();
for(auto &n:sTop->neighbors){
if(m.count(n)==0){
Node *temp=new Node(n->val);
m[n]=temp;
s.push(n);
}
m[sTop]->neighbors.push_back(m[n]);
}
}
return newNode;
}
};
232. 用栈实现队列
class MyQueue {
stack<int>sIn,sOut;
public:
/** Initialize your data structure here. */
MyQueue() {
;
}
/** Push element x to the back of queue. */
void push(int x) {
sIn.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
if(sOut.empty())
for(int i=0,j=sIn.size();i<j;++i){
sOut.push(sIn.top());
sIn.pop();
}
int res=sOut.top();
sOut.pop();
return res;
}
/** Get the front element. */
int peek() {
if(sOut.empty())
for(int i=0,j=sIn.size();i<j;++i){
sOut.push(sIn.top());
sIn.pop();
}
return sOut.top();
}
/** Returns whether the queue is empty. */
bool empty() {
return sIn.empty()&&sOut.empty();
}
};
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue* obj = new MyQueue();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->peek();
* bool param_4 = obj->empty();
*/
class MyStack {
queue<int>q;
public:
/** Initialize your data structure here. */
MyStack() {
;
}
/** Push element x onto stack. */
void push(int x) {
q.push(x);
}
/** Removes the element on top of the stack and returns that element. */
int pop() {
for(int i=1,qSize=q.size();i<qSize;++i){
q.push(q.front());
q.pop();
}
int res=q.front();
q.pop();
return res;
}
/** Get the top element. */
int top() {
return q.back();
}
/** Returns whether the stack is empty. */
bool empty() {
return q.size()==0;
}
};
/**
* Your MyStack object will be instantiated and called as such:
* MyStack* obj = new MyStack();
* obj->push(x);
* int param_2 = obj->pop();
* int param_3 = obj->top();
* bool param_4 = obj->empty();
*/