leetcode Ch7-Graph Search
1. Clone Graph
BFS:
class Solution {
public:
typedef UndirectedGraphNode UGNode;
UndirectedGraphNode* cloneGraph(UndirectedGraphNode* node) {
if (node == NULL) {
return NULL;
}
vector<UGNode*> nodes;
unordered_map<UGNode*, UGNode*> umap; //clone nodes only
nodes.push_back(node);
umap[node] = new UGNode(node->label);
int start = ;
while (start < nodes.size()) {
UGNode* tmp = nodes[start];
start++;
for (int i = ; i < tmp->neighbors.size(); i++) {
if (umap.count(tmp->neighbors[i]) == ) {
nodes.push_back(tmp->neighbors[i]);
umap[tmp->neighbors[i]] = new UGNode(tmp->neighbors[i]->label);
}
}
} //clone neighbors only
for (int i = ; i < nodes.size(); i++) {
UGNode* newNode = umap[nodes[i]];
for (int j = ; j < nodes[i]->neighbors.size(); j++) {
newNode->neighbors.push_back(umap[nodes[i]->neighbors[j]]);
}
}
return umap[node];
}
};
DFS:
class Solution {
public:
typedef UndirectedGraphNode UGNode;
UndirectedGraphNode* cloneGraph(UndirectedGraphNode* node) {
if (node == NULL) {
return NULL;
}
return dfs(node);
} UGNode* dfs(UGNode* node) {
UGNode* p = new UGNode(node->label);
umap[p->label] = p;
for (int i = ; i < node->neighbors.size(); i++) {
if (umap.count(node->neighbors[i]->label) == ) {
p->neighbors.push_back(dfs(node->neighbors[i]));
} else {
p->neighbors.push_back(umap[node->neighbors[i]->label]);
}
}
return p;
}
private:
unordered_map<int, UGNode*> umap;
};
这里umap里的second存的都是copy后的图节点,不是原图。
图的邻接表DFS:
struct VertexNode {
int data;
EdgeNode* next;
}; struct EdgeNode {
int index; // 存储其对应的点的下标
EdgeNode* next;
}; struct Graph {
VertexNode vexs[MAXVEX];
int numVertex;
}; vector<bool> visited; void dfs(Graph* g, int i) {
visited[i] = ;
print(g, i);
EdgeNode* p = g->vexs[i]->next;
while (p) {
if (!visited[p->index]) {
dfs(g, p->index);
}
p = p->next;
}
} void dfsTraverse(Graph* g) {
for (int i = ; i < g->numVertex; i++) {
if (!visited[i]) {
dfs(g, i);
}
}
}
图的邻接表BFS:
void bfsTraverse(Graph* g) {
queue<int> q;
for (int i = ; i < numVertex; i++) {
if (!visited[i]) {
visited[i] = ;
print(g, i);
q.push(i);
while (!q.empty()) {
int j = q.top();
q.pop();
EdgeNode* p = g->vexs[j].next;
while (p) {
if (!visited[p->index]) {
visited[p->index] = ;
print(g, p->index);
q.push(p->index);
}
p = p->next;
}
}
}
}
}
leetcode Ch7-Graph Search的更多相关文章
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
- LeetCode: Validata Binary Search Tree
LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search ...
- Graph Search图谱搜索
来自百度百科的解释: Graph Search为2013年1月16日,Facebook首席执行官马克·扎克伯格(Mark Zuckerberg)在门罗帕克公司总部召开的新闻发布会上宣布推出社交搜索工具 ...
- 【一天一道LeetCode】#81. Search in Rotated Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- [LeetCode] 79. Word Search 单词搜索
Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...
- [LeetCode] 212. Word Search II 词语搜索 II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
随机推荐
- 【Guava】Optional接口来避免空指针错误
null会带来很多问题,从开始有null开始有无数程序栽在null的手里,null的含义是不清晰的,检查null在大多数情况下是不得不做的,而我们又在很多时候忘记了对null做检查,在我们的产品真正投 ...
- 解析XML:DOM,SAX,PULL
Android解析XML有三种方式:DOM(document object model).SAX(simple api XML).PULL 1.DOM DOM解析XML文件时,会将XML文件的所有内容 ...
- 《LeetBook》LeetCode题解(2):Add Two Numbers [M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- python-UDP传输模型
#!/usr/bin/python #coding=utf-8 #服务器端 from socket import * from time import ctime HOST="192.168 ...
- PTA (Advanced Level) 1014 Waiting in Line
Waiting in Line Suppose a bank has N windows open for service. There is a yellow line in front of th ...
- spark2.4+elasticsearch6.1.1搭建一个推荐系统
目录 整体框架 安装相关的组件 elasticsearch安装 spark安装 下载es-hadoop中间件 安装计算向量相似度的elasticsearch插件 运行 结果展示 参考博客 本博文详细记 ...
- unity简易ui框架
在unity项目开发中,ui模块的开发往往占据了很大一部分工作,部分游戏甚至绝大部分的工作都是在ui上,如何高效管理各种界面,这里分享一套高效易用的UI框架. 首先,我们定义一个PanelBase类, ...
- IntelliJ使用指南—— 导入Eclipse的Web项目
通常一个团队中可能有人用eclipse,有人用intelliJ,那么经常会出现需要导入别人用eclipse建好的web项目.而IntelliJ提供了多种项目类型的导入方式,其中就有eclipse. 在 ...
- 删除Panl控件中窗体的方法
//删除窗体方法 private void CloseFrm() { foreach (Control item in panel1.Controls) { if (item is Form) //判 ...
- Knockout.js Visible绑定
<head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8&quo ...