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 ...
随机推荐
- LoginForm表单的执行过程
读取这篇文章,您将了解到 提前熟悉几个基础点 LoginForm表单的执行过程 首先我们看表单模型 声明验证规则 填充模型 触发验证 默认的用户密码加密 用户验证中使用Salt 数据验证 调试Yii ...
- MYSQL 缓存
在PHP.INI中query_cache_type设置为1. 即 开始MYSQL全局SQL语句 都缓存.(0 不使用) 临时关闭查询缓冲的方法:1. SELECT SQL_NO_CACHE fi ...
- JavaScript设计模式-21.命令模式
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- C 标准库 - ctype.h之isalnum使用
isalnum int isalnum ( int c ); Checks whether c is either a decimal digit or an uppercase or lowerca ...
- git删除远程主机没有的tag
可以先删除所有本地tag,然后再拉取远程上的tag git tag -l | xargs git tag -d git fetch --tags 其他方法以及查询tag的命令请见:Remove loc ...
- FocusBI: SSIS 开发案例(原创)
关注微信公众号:FocusBI 查看更多文章:加QQ群:808774277 获取学习资料和一起探讨问题. <商业智能教程>pdf下载地址 链接:https://pan.baidu.com/ ...
- python实例:快速找出多个字典中的公共键
1.生成随机字典 # 从abcdefg 中随机取出 3-6个,作为key, 1-4 的随机数作为 value s1 = {x : randint(1, 4) for x in sample('abcd ...
- 码表的理解(ASCII,GBK,Unicode,UTF-8等)。
以下任何言论都完全是个人的理解,如有雷同纯属巧合,如有错误,希望大家多多指出,共同学习!谢谢! 笔者是一个理解能力偏慢.稍钻牛角尖的程序员,什么东西都要从最基础理解起,一步一步向上理解,因此讲述时也是 ...
- nodejs学习笔记一( sublime、atom开发环境,http模块,fs模块的初识)
http服务 let server = http.createServer(function(req,res){ }); 监听: server.listen(8080); re ...
- [转]Support Composite Key in ASP.NET Web API OData
本文转自:https://code.msdn.microsoft.com/Support-Composite-Key-in-d1d53161 he default EntitySetControlle ...