forked from lzl124631x/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f99eedf
Showing
222 changed files
with
6,747 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.exe | ||
*.exe.stackdump |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
#include <iostream> | ||
#include <queue> | ||
#include <vector> | ||
using namespace std; | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
struct TreeNode { | ||
int val; | ||
TreeNode *left; | ||
TreeNode *right; | ||
TreeNode(int x) : val(x), left(NULL), right(NULL) {} | ||
}; | ||
|
||
vector<int> breathFirstTraversal(TreeNode *root){ | ||
if(!root) return vector<int>(); | ||
vector<int> v; | ||
queue<TreeNode*> q; | ||
q.push(root); | ||
int num = 1; | ||
while(num){ | ||
int nextNum = 0; | ||
while(num--){ | ||
TreeNode *n = q.front(); q.pop(); | ||
v.push_back(n->val); | ||
if(n->left){ | ||
nextNum++; | ||
q.push(n->left); | ||
} | ||
if(n->right){ | ||
nextNum++; | ||
q.push(n->right); | ||
} | ||
} | ||
num = nextNum; | ||
} | ||
return v; | ||
} | ||
|
||
// Note: Invalid input is not considered yet. | ||
TreeNode* deserialize(string str){ | ||
char *s = (char*)str.c_str(), *p = NULL; | ||
p = strsep(&s, ","); | ||
if(!strcmp(p, "#")) return NULL; | ||
TreeNode *root = new TreeNode(atoi(p)); | ||
queue<TreeNode*> q; | ||
q.push(root); | ||
while(!q.empty()){ | ||
TreeNode *pnode = q.front(); q.pop(); | ||
TreeNode *n[2] = {NULL}; | ||
for(int i = 0; s && i < 2; ++i){ | ||
p = strsep(&s, ","); | ||
if(strcmp(p, "#")){ | ||
n[i] = new TreeNode(atoi(p)); | ||
q.push(n[i]); | ||
} | ||
} | ||
pnode->left = n[0]; | ||
pnode->right = n[1]; | ||
} | ||
return root; | ||
} | ||
|
||
string serialize(TreeNode* root){ | ||
if(!root) return ""; | ||
char buf[100000] = {0}, *p = buf; | ||
queue<TreeNode*> q; | ||
q.push(root); | ||
while(!q.empty()){ | ||
TreeNode *n = q.front(); q.pop(); | ||
if(n){ | ||
p += sprintf(p, "%d,", n->val); | ||
}else{ | ||
p += sprintf(p, "#,"); | ||
} | ||
if(n){ | ||
q.push(n->left); | ||
q.push(n->right); | ||
} | ||
} | ||
while(*(p - 1) == ',' && *(p - 2) == '#') p -= 2; | ||
*(p - 1) = '\0'; | ||
return string(buf); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include "BinaryTree.h" | ||
|
||
int main(){ | ||
string s = "3,9,20,#,#,15,7"; | ||
TreeNode *root = deserialize(s); | ||
printVector(breathFirstTraversal(root)); | ||
cout << serialize(root) << endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#ifndef Helper_h | ||
#define Helper_h | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include <iostream> | ||
#include <vector> | ||
#include <string> | ||
using namespace std; | ||
#define CASET int ___T, case_n = 1; scanf("%d ", &___T); while (___T-- > 0) | ||
#define PRINTCASE printf("Case #%d: ",case_n++) | ||
#define PRINTCASE_ printf("Case #%d:\n",case_n++) | ||
#define RD(a) scanf("%d", &(a)) | ||
#define RDD(a, b) scanf("%d%d", &(a), &(b)) | ||
|
||
#define CLK_INIT clock_t clk_start = 0, clk_end = 0 | ||
#define CLK_START clk_start = clock() | ||
#define CLK_END\ | ||
do{ clk_end = clock();\ | ||
cout << "Time[" << (double)(clk_end - clk_start) / CLOCKS_PER_SEC << "s]" << endl; }while(0) | ||
|
||
template<typename T> | ||
void printVector(vector<T> v){ | ||
typename vector<T>::iterator i = v.begin(); | ||
for(; i != v.end(); ++i){ | ||
cout << "[" << *i << "] "; | ||
} | ||
cout << endl; | ||
} | ||
|
||
template<typename T> | ||
void printVector(vector<vector<T> > v){ | ||
for(int i = 0; i < v.size(); ++i){ | ||
for(int j = 0; j < v[i].size(); ++j){ | ||
cout << "[" << v[i][j] << "] "; | ||
} | ||
cout << endl; | ||
} | ||
} | ||
|
||
vector<vector<int> > scanVector(int m, int n){ | ||
vector<vector<int> > v; | ||
for(int i = 0; i < m; ++i){ | ||
vector<int> v1; | ||
for(int j = 0; j < n; ++j){ | ||
int d; | ||
cin >> d; | ||
v1.push_back(d); | ||
} | ||
v.push_back(v1); | ||
} | ||
return v; | ||
} | ||
|
||
vector<int> scanVector(){ | ||
vector<int> v; | ||
string str; | ||
cin >> str; | ||
char *s = (char*) str.c_str(), *p = NULL; | ||
while(p = strsep(&s, ",")){ | ||
v.push_back(atoi(p)); | ||
} | ||
return v; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#ifndef SinglyLinkedList_h | ||
#define SinglyLinkedList_h | ||
struct ListNode{ | ||
int val; | ||
ListNode *next; | ||
ListNode(int x) : val(x), next(NULL) {} | ||
}; | ||
|
||
void list_destroy(ListNode *head){ | ||
if(!head) return; | ||
ListNode *next; | ||
while(head){ | ||
next = head->next; | ||
delete head; | ||
head = next; | ||
} | ||
} | ||
|
||
void listnode_append(ListNode *head, int val){ | ||
if(!head) return; | ||
while(head->next){ | ||
head = head->next; | ||
} | ||
head->next = new ListNode(val); | ||
} | ||
|
||
void print_list(ListNode *head){ | ||
while(head){ | ||
printf("[%d] ", head->val); | ||
head = head->next; | ||
} | ||
printf("\n"); | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#include <iostream> | ||
#include <queue> | ||
#include <string> | ||
using namespace std; | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
struct TreeLinkNode { | ||
int val; | ||
TreeLinkNode *left, *right, *next; | ||
TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {} | ||
}; | ||
|
||
// Note: Invalid input is not considered yet. | ||
TreeLinkNode* deserialize(string str){ | ||
char *s = (char*)str.c_str(), *p = NULL; | ||
p = strsep(&s, ","); | ||
if(!strcmp(p, "#")) return NULL; | ||
TreeLinkNode *root = new TreeLinkNode(atoi(p)); | ||
queue<TreeLinkNode*> q; | ||
q.push(root); | ||
while(!q.empty()){ | ||
TreeLinkNode *pnode = q.front(); q.pop(); | ||
TreeLinkNode *n[2] = {NULL}; | ||
for(int i = 0; s && i < 2; ++i){ | ||
p = strsep(&s, ","); | ||
if(strcmp(p, "#")){ | ||
n[i] = new TreeLinkNode(atoi(p)); | ||
q.push(n[i]); | ||
} | ||
} | ||
pnode->left = n[0]; | ||
pnode->right = n[1]; | ||
} | ||
return root; | ||
} | ||
|
||
string serialize(TreeLinkNode* root){ | ||
if(!root) return ""; | ||
char buf[100000] = {0}, *p = buf; | ||
TreeLinkNode *nextLv = root; | ||
while(nextLv){ | ||
root = nextLv; | ||
nextLv = NULL; | ||
while(root){ | ||
p += sprintf(p, "%d,", root->val); | ||
if(!nextLv && (root->left || root->right)){ | ||
nextLv = root->left ? root->left : root->right; | ||
} | ||
root = root->next; | ||
} | ||
*(p - 1) = '#'; | ||
} | ||
*(p - 1) = '\0'; | ||
return string(buf); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#include <iostream> | ||
#include <queue> | ||
#include <vector> | ||
#include <set> | ||
#include <map> | ||
using namespace std; | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
struct UndirectedGraphNode { | ||
int label; | ||
vector<UndirectedGraphNode *> neighbors; | ||
UndirectedGraphNode(int x) : label(x) {}; | ||
}; | ||
|
||
// set中不应该存储"已经被访问的节点", 因为这可能导致队列中出现重复的待访问节点. | ||
// set中应该存储"已经插入过队列的节点", 也就是紧随q.push(n)操作. | ||
vector<int> breathFirstTraversal(UndirectedGraphNode *n){ | ||
if(!n) return vector<int>(); | ||
vector<int> v; | ||
set<int> s; | ||
queue<UndirectedGraphNode*> q; | ||
q.push(n); | ||
s.insert(n->label); | ||
while(!q.empty()){ | ||
n = q.front(); q.pop(); | ||
v.push_back(n->label); | ||
vector<UndirectedGraphNode*>::iterator i; | ||
i = n->neighbors.begin(); | ||
for(; i != n->neighbors.end(); ++i){ | ||
if(s.find((*i)->label) == s.end()){ | ||
q.push(*i); | ||
s.insert((*i)->label); | ||
} | ||
} | ||
} | ||
return v; | ||
} | ||
|
||
void printVector(vector<int> v){ | ||
vector<int>:: iterator i = v.begin(); | ||
for(; i != v.end(); ++i){ | ||
cout << "[" << *i << "] "; | ||
} | ||
cout << endl; | ||
} | ||
|
||
// Note: Invalid input is not considered yet. | ||
UndirectedGraphNode* deserialize(string str){ | ||
char *s = (char*)str.c_str(), *p = NULL; | ||
map<int, UndirectedGraphNode*> m; | ||
UndirectedGraphNode *ret = NULL; | ||
while(p = strsep(&s, "#")){ | ||
char *q = NULL; | ||
UndirectedGraphNode *n = NULL; | ||
vector<UndirectedGraphNode*> v; | ||
while(q = strsep(&p, ",")){ | ||
int label = atoi(q); | ||
if(m.find(label) != m.end()){ | ||
n = m[label]; | ||
}else{ | ||
n = new UndirectedGraphNode(label); | ||
m[label] = n; | ||
} | ||
v.push_back(n); | ||
} | ||
n = v.front(); | ||
v.erase(v.begin()); | ||
n->neighbors = v; | ||
if(!ret){ | ||
ret = n; | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
string serialize(UndirectedGraphNode* n){ | ||
if(!n) return ""; | ||
char buf[1000] = {0}, *p = buf; | ||
set<int> s; | ||
queue<UndirectedGraphNode*> q; | ||
q.push(n); | ||
s.insert(n->label); | ||
while(!q.empty()){ | ||
n = q.front(); q.pop(); | ||
p += sprintf(p, "%d,", n->label); | ||
vector<UndirectedGraphNode*>::iterator i; | ||
i = n->neighbors.begin(); | ||
for(; i != n->neighbors.end(); ++i){ | ||
int label = (*i)->label; | ||
p += sprintf(p, "%d,", label); | ||
if(s.find(label) == s.end()){ | ||
q.push(*i); | ||
s.insert(label); | ||
} | ||
} | ||
*(p - 1) = '#'; | ||
} | ||
*(p - 1) = '\0'; | ||
return string(buf); | ||
} | ||
|
||
int main(){ | ||
string s = "0,1,2#1,2#2,2"; | ||
UndirectedGraphNode *n = deserialize(s); | ||
printVector(breathFirstTraversal(n)); | ||
cout << serialize(n) << endl; | ||
return 0; | ||
} |
Oops, something went wrong.