mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-24 02:03:10 +08:00
Rearrange the chapters.
Start to translate codes from Java to Python.
This commit is contained in:
50
codes/cpp/include/ListNode.hpp
Normal file
50
codes/cpp/include/ListNode.hpp
Normal file
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* File: PrintUtil.hpp
|
||||
* Created Time: 2021-12-19
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
/**
|
||||
* @brief Definition for a singly-linked list node
|
||||
*
|
||||
*/
|
||||
struct ListNode {
|
||||
int val;
|
||||
ListNode *next;
|
||||
ListNode(int x) : val(x), next(nullptr) {}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Generate a linked list with a vector
|
||||
*
|
||||
* @param list
|
||||
* @return ListNode*
|
||||
*/
|
||||
ListNode* vectorToLinkedList(vector<int> list) {
|
||||
ListNode *dum = new ListNode(0);
|
||||
ListNode *head = dum;
|
||||
for (int val : list) {
|
||||
head->next = new ListNode(val);
|
||||
head = head->next;
|
||||
}
|
||||
return dum->next;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get a list node with specific value from a linked list
|
||||
*
|
||||
* @param head
|
||||
* @param val
|
||||
* @return ListNode*
|
||||
*/
|
||||
ListNode* getListNode(ListNode *head, int val) {
|
||||
while (head != nullptr && head->val != val) {
|
||||
head = head->next;
|
||||
}
|
||||
return head;
|
||||
}
|
197
codes/cpp/include/PrintUtil.hpp
Normal file
197
codes/cpp/include/PrintUtil.hpp
Normal file
@ -0,0 +1,197 @@
|
||||
/*
|
||||
* File: PrintUtil.hpp
|
||||
* Created Time: 2021-12-19
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include "ListNode.hpp"
|
||||
#include "TreeNode.hpp"
|
||||
|
||||
class PrintUtil {
|
||||
public:
|
||||
/**
|
||||
* @brief Find an element in a vector
|
||||
*
|
||||
* @tparam T
|
||||
* @param vec
|
||||
* @param ele
|
||||
* @return int
|
||||
*/
|
||||
template <typename T>
|
||||
static int vecFind(const vector<T>& vec, T ele) {
|
||||
int j = INT_MAX;
|
||||
for (int i = 0; i < vec.size(); i++) {
|
||||
if (vec[i] == ele) {
|
||||
j = i;
|
||||
}
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Concatenate a vector with a delim
|
||||
*
|
||||
* @tparam T
|
||||
* @param delim
|
||||
* @param vec
|
||||
* @return string
|
||||
*/
|
||||
template <typename T>
|
||||
static string strJoin(const string& delim, const T& vec) {
|
||||
ostringstream s;
|
||||
for (const auto& i : vec) {
|
||||
if (&i != &vec[0]) {
|
||||
s << delim;
|
||||
}
|
||||
s << i;
|
||||
}
|
||||
return s.str();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Repeat a string for n times
|
||||
*
|
||||
* @param str
|
||||
* @param n
|
||||
* @return string
|
||||
*/
|
||||
static string strRepeat(string str, int n) {
|
||||
ostringstream os;
|
||||
for(int i = 0; i < n; i++)
|
||||
os << str;
|
||||
return os.str();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the Vector String object
|
||||
*
|
||||
* @tparam T
|
||||
* @param list
|
||||
* @return string
|
||||
*/
|
||||
template <typename T>
|
||||
static string getVectorString(vector<T> &list) {
|
||||
return "[" + strJoin(", ", list) + "]";
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a vector
|
||||
*
|
||||
* @tparam T
|
||||
* @param list
|
||||
*/
|
||||
template <typename T>
|
||||
static void printVector(vector<T> &list) {
|
||||
cout << getVectorString(list) << '\n';
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a vector matrix
|
||||
*
|
||||
* @tparam T
|
||||
* @param matrix
|
||||
*/
|
||||
template <typename T>
|
||||
static void printVectorMatrix(vector<vector<T>> &matrix) {
|
||||
cout << "[" << '\n';
|
||||
for (vector<T> &list : matrix)
|
||||
cout << " " + getVectorString(list) + "," << '\n';
|
||||
cout << "]" << '\n';
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a linked list
|
||||
*
|
||||
* @param head
|
||||
*/
|
||||
static void printLinkedList(ListNode *head) {
|
||||
vector<int> list;
|
||||
while (head != nullptr) {
|
||||
list.push_back(head->val);
|
||||
head = head->next;
|
||||
}
|
||||
|
||||
cout << strJoin(" -> ", list) << '\n';
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This tree printer is borrowed from TECHIE DELIGHT
|
||||
* https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
*/
|
||||
struct Trunk {
|
||||
Trunk *prev;
|
||||
string str;
|
||||
Trunk(Trunk *prev, string str) {
|
||||
this->prev = prev;
|
||||
this->str = str;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Helper function to print branches of the binary tree
|
||||
*
|
||||
* @param p
|
||||
*/
|
||||
static void showTrunks(Trunk *p) {
|
||||
if (p == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
showTrunks(p->prev);
|
||||
cout << p->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The interface of the tree printer
|
||||
*
|
||||
* @param root
|
||||
*/
|
||||
static void printTree(TreeNode *root) {
|
||||
printTree(root, nullptr, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Print a binary tree
|
||||
*
|
||||
* @param root
|
||||
* @param prev
|
||||
* @param isLeft
|
||||
*/
|
||||
static void printTree(TreeNode *root, Trunk *prev, bool isLeft) {
|
||||
if (root == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
string prev_str = " ";
|
||||
Trunk *trunk = new Trunk(prev, prev_str);
|
||||
|
||||
printTree(root->right, trunk, true);
|
||||
|
||||
if (!prev) {
|
||||
trunk->str = "———";
|
||||
}
|
||||
else if (isLeft) {
|
||||
trunk->str = "/———";
|
||||
prev_str = " |";
|
||||
}
|
||||
else {
|
||||
trunk->str = "\\———";
|
||||
prev->str = prev_str;
|
||||
}
|
||||
|
||||
showTrunks(trunk);
|
||||
cout << " " << root->val << endl;
|
||||
|
||||
if (prev) {
|
||||
prev->str = prev_str;
|
||||
}
|
||||
trunk->str = " |";
|
||||
|
||||
printTree(root->left, trunk, false);
|
||||
}
|
||||
};
|
63
codes/cpp/include/TreeNode.hpp
Normal file
63
codes/cpp/include/TreeNode.hpp
Normal file
@ -0,0 +1,63 @@
|
||||
/*
|
||||
* File: PrintUtil.hpp
|
||||
* Created Time: 2021-12-19
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* @brief Definition for a binary tree node
|
||||
*
|
||||
*/
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Generate a binary tree with a vector
|
||||
*
|
||||
* @param list
|
||||
* @return TreeNode*
|
||||
*/
|
||||
TreeNode* vectorToTree(vector<int> list) {
|
||||
TreeNode *root = new TreeNode(list[0]);
|
||||
queue<TreeNode*> que;
|
||||
que.emplace(root);
|
||||
int i = 1;
|
||||
while(!que.empty()) {
|
||||
TreeNode *node = que.front();
|
||||
que.pop();
|
||||
if(list[i] != INT_MAX) {
|
||||
node->left = new TreeNode(list[i]);
|
||||
que.emplace(node->left);
|
||||
}
|
||||
i++;
|
||||
if(list[i] != INT_MAX) {
|
||||
node->right = new TreeNode(list[i]);
|
||||
que.emplace(node->right);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get a tree node with specific value in a binary tree
|
||||
*
|
||||
* @param root
|
||||
* @param val
|
||||
* @return TreeNode*
|
||||
*/
|
||||
TreeNode* getTreeNode(TreeNode *root, int val) {
|
||||
if (root == nullptr)
|
||||
return nullptr;
|
||||
if (root->val == val)
|
||||
return root;
|
||||
TreeNode *left = getTreeNode(root->left, val);
|
||||
TreeNode *right = getTreeNode(root->right, val);
|
||||
return left != nullptr ? left : right;
|
||||
}
|
23
codes/cpp/include/include.hpp
Normal file
23
codes/cpp/include/include.hpp
Normal file
@ -0,0 +1,23 @@
|
||||
/*
|
||||
* File: PrintUtil.hpp
|
||||
* Created Time: 2021-12-19
|
||||
* Author: Krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <stack>
|
||||
#include <queue>
|
||||
#include <deque>
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <set>
|
||||
|
||||
#include "ListNode.hpp"
|
||||
#include "TreeNode.hpp"
|
||||
#include "PrintUtil.hpp"
|
||||
|
||||
using namespace std;
|
Reference in New Issue
Block a user