mirror of
https://git.wolves.top/wolves/leetcode.git
synced 2025-11-04 17:26:32 +08:00
init
This commit is contained in:
181
else/structs/Graph.h
Normal file
181
else/structs/Graph.h
Normal file
@@ -0,0 +1,181 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/10/21.
|
||||
// 以邻接矩阵实现的INT图及其具体操作
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_GRAPH_H
|
||||
#define LEECODE_C_GRAPH_H
|
||||
|
||||
#include <vector>
|
||||
#include <set>
|
||||
#include <map>
|
||||
#include <queue>
|
||||
#include <random>
|
||||
#include <iostream>
|
||||
#include <stack>
|
||||
|
||||
using namespace std;
|
||||
|
||||
class Graph {
|
||||
public:
|
||||
int size;
|
||||
|
||||
Graph(int n, bool direction) {
|
||||
this->size = n;
|
||||
G = vector<vector<int>>(size, vector<int>(size, 0));
|
||||
visited = vector<int>(size, 0);
|
||||
|
||||
random_device rd;
|
||||
mt19937 gen(rd());
|
||||
uniform_int_distribution<int> dis(0, 1);
|
||||
|
||||
if (!direction) {
|
||||
for (int i = 0; i < size; ++i) {
|
||||
for (int j = i + 1; j < size; ++j) {
|
||||
if (dis(gen)) {
|
||||
G[i][j] = 1;
|
||||
G[j][i] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < size; ++i) {
|
||||
for (int j = 0; j < size; ++j) {
|
||||
if (i == j) {
|
||||
continue;
|
||||
}
|
||||
if (dis(gen)) {
|
||||
G[i][j] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 0 1 0 0 0 0
|
||||
// 1 0 1 1 0 0
|
||||
// 0 1 0 0 0 1
|
||||
// 0 1 0 0 0 0
|
||||
// 0 0 0 0 0 1
|
||||
// 0 0 1 0 1 0
|
||||
Graph(int x) {
|
||||
size = 6;
|
||||
G = vector<vector<int>>(size, vector<int>(size, 0));
|
||||
visited = vector<int>(size, 0);
|
||||
vector<vector<int>> Edges;
|
||||
if (x == 1) {
|
||||
Edges.push_back({0, 1});
|
||||
Edges.push_back({1, 2});
|
||||
Edges.push_back({1, 3});
|
||||
Edges.push_back({2, 5});
|
||||
Edges.push_back({4, 5});
|
||||
}
|
||||
if (x == 2) {
|
||||
|
||||
}
|
||||
for (auto Edge: Edges) {
|
||||
G[Edge[0]][Edge[1]] = 1;
|
||||
G[Edge[1]][Edge[0]] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
Graph *GraphByEdge(vector<vector<int>> &Edges) {
|
||||
for (auto Edge: Edges) {
|
||||
vertex.insert(Edge[1]);
|
||||
vertex.insert(Edge[2]);
|
||||
}
|
||||
|
||||
size = vertex.size();
|
||||
G = vector<vector<int>>(size, vector<int>(size, 0));
|
||||
|
||||
for (auto Edge: Edges) {
|
||||
G[Edge[1]][Edge[2]] = 1;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
void printG() {
|
||||
for (int i = 0; i < size; ++i) {
|
||||
for (int j = 0; j < size; ++j) {
|
||||
cout << G[i][j] << " ";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
queue<int> DFS(int start) {
|
||||
queue<int> Q;
|
||||
stack<pair<int, int>> S;
|
||||
Q.push(start);
|
||||
S.emplace(start, -1);
|
||||
visited[0] = 1;
|
||||
while (!S.empty()) {
|
||||
auto temp = S.top();
|
||||
auto next = NextPoint(temp.first, temp.second);
|
||||
S.top().second = next;
|
||||
if (next == -1) {
|
||||
S.pop();
|
||||
continue;
|
||||
}
|
||||
if (visited[next]) {
|
||||
continue;
|
||||
}
|
||||
Q.push(next);
|
||||
visited[next] = 1;
|
||||
S.emplace(next, -1);
|
||||
}
|
||||
if (Q.size() != size) {
|
||||
// 将其他的点再进行DSF
|
||||
}
|
||||
return Q;
|
||||
}
|
||||
|
||||
queue<int> BFS(int start) {
|
||||
queue<int> Q;
|
||||
queue<pair<int, int>> S;
|
||||
Q.push(start);
|
||||
S.emplace(start, -1);
|
||||
visited[0] = 1;
|
||||
while (!S.empty()) {
|
||||
auto temp = S.front();
|
||||
auto next = NextPoint(temp.first, temp.second);
|
||||
S.front().second = next;
|
||||
if (next == -1) {
|
||||
S.pop();
|
||||
continue;
|
||||
}
|
||||
if (visited[next]) {
|
||||
continue;
|
||||
}
|
||||
Q.push(next);
|
||||
visited[next] = 1;
|
||||
S.emplace(next, -1);
|
||||
}
|
||||
if (Q.size() != size) {
|
||||
// 将其他的点再进行BSF
|
||||
}
|
||||
return Q;
|
||||
}
|
||||
|
||||
int NextPoint(int vex, int now) {
|
||||
auto row = G[vex];
|
||||
for (int i = now + 1; i < size; ++i) {
|
||||
if (row[i] != 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private:
|
||||
vector<vector<int>> G; // adjacent matrix
|
||||
vector<int> visited;
|
||||
|
||||
set<int> vertex; // The collection of vertex
|
||||
set<set<int>> ccp; // The graph has multiple connected components.
|
||||
|
||||
|
||||
};
|
||||
|
||||
#endif //LEECODE_C_GRAPH_H
|
||||
8
else/structs/List.h
Normal file
8
else/structs/List.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#ifndef List_h
|
||||
#define List_h
|
||||
struct ListNode{
|
||||
int value;
|
||||
struct ListNode * next;
|
||||
}
|
||||
|
||||
#endif //List_h
|
||||
94
else/structs/Tree.h
Normal file
94
else/structs/Tree.h
Normal file
@@ -0,0 +1,94 @@
|
||||
//
|
||||
// Created by 李洋 on 2023/8/14.
|
||||
//
|
||||
|
||||
#ifndef LEECODE_C_TREE_H
|
||||
#define LEECODE_C_TREE_H
|
||||
|
||||
struct TreeNode
|
||||
{
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
|
||||
TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
|
||||
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
|
||||
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
|
||||
};
|
||||
|
||||
#include <iostream>
|
||||
#include <random>
|
||||
#include <queue>
|
||||
|
||||
TreeNode *createRandomTree(int size)
|
||||
{
|
||||
if (size == 0)
|
||||
{
|
||||
return nullptr;
|
||||
}
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
std::uniform_int_distribution<int> dis(1, 100);
|
||||
|
||||
std::queue<int> Q;
|
||||
std::queue<TreeNode *> T;
|
||||
for (int i = size; i > 0; --i)
|
||||
{
|
||||
Q.push(dis(gen));
|
||||
std::cout << Q.back() << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
TreeNode *head = new TreeNode(Q.front());
|
||||
T.push(head);
|
||||
Q.pop();
|
||||
|
||||
while (!Q.empty())
|
||||
{
|
||||
if (T.front()->left != nullptr && T.front()->right != nullptr)
|
||||
{
|
||||
T.pop();
|
||||
}
|
||||
|
||||
TreeNode *temp = new TreeNode(Q.front());
|
||||
Q.pop();
|
||||
|
||||
if (T.front()->left == nullptr)
|
||||
{
|
||||
T.front()->left = temp;
|
||||
T.push(temp);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (T.front()->right == nullptr)
|
||||
{
|
||||
T.front()->right = temp;
|
||||
T.push(temp);
|
||||
}
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
void lookTree(TreeNode *root)
|
||||
{
|
||||
std::queue<TreeNode *> q;
|
||||
q.push(root);
|
||||
while (!q.empty())
|
||||
{
|
||||
TreeNode *head = q.front();
|
||||
q.pop();
|
||||
std::cout << head->val << " ";
|
||||
if (head->left)
|
||||
{
|
||||
q.push(head->left);
|
||||
}
|
||||
if (head->right)
|
||||
{
|
||||
q.push(head->right);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif // LEECODE_C_TREE_H
|
||||
Reference in New Issue
Block a user