mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-22 03:32:42 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -30,7 +30,7 @@ ListNode *vecToLinkedList(vector<int> list) {
|
||||
return dum->next;
|
||||
}
|
||||
|
||||
/* Free memory allocated to linked list */
|
||||
/* Free the memory allocated to the linked list */
|
||||
void freeMemoryLinkedList(ListNode *cur) {
|
||||
// Free memory
|
||||
ListNode *pre;
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
/* Find an element in a vector */
|
||||
/* Find the last matching element's index in a vector */
|
||||
template <typename T> int vecFind(const vector<T> &vec, T ele) {
|
||||
int j = INT_MAX;
|
||||
for (int i = 0; i < vec.size(); i++) {
|
||||
@@ -24,7 +24,7 @@ template <typename T> int vecFind(const vector<T> &vec, T ele) {
|
||||
return j;
|
||||
}
|
||||
|
||||
/* Concatenate a vector with a delim */
|
||||
/* Join vector elements with a delimiter */
|
||||
template <typename T> string strJoin(const string &delim, const T &vec) {
|
||||
ostringstream s;
|
||||
for (const auto &i : vec) {
|
||||
@@ -36,7 +36,7 @@ template <typename T> string strJoin(const string &delim, const T &vec) {
|
||||
return s.str();
|
||||
}
|
||||
|
||||
/* Repeat a string for n times */
|
||||
/* Repeat a string n times */
|
||||
string strRepeat(string str, int n) {
|
||||
ostringstream os;
|
||||
for (int i = 0; i < n; i++)
|
||||
@@ -44,7 +44,7 @@ string strRepeat(string str, int n) {
|
||||
return os.str();
|
||||
}
|
||||
|
||||
/* Print array */
|
||||
/* Print an array */
|
||||
template <typename T> void printArray(T *arr, int n) {
|
||||
cout << "[";
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
@@ -56,17 +56,17 @@ template <typename T> void printArray(T *arr, int n) {
|
||||
cout << "]" << endl;
|
||||
}
|
||||
|
||||
/* Get the Vector String object */
|
||||
/* Get a string representation of a vector */
|
||||
template <typename T> string getVectorString(vector<T> &list) {
|
||||
return "[" + strJoin(", ", list) + "]";
|
||||
}
|
||||
|
||||
/* Print list */
|
||||
/* Print the list */
|
||||
template <typename T> void printVector(vector<T> list) {
|
||||
cout << getVectorString(list) << '\n';
|
||||
}
|
||||
|
||||
/* Print matrix */
|
||||
/* Print a matrix */
|
||||
template <typename T> void printVectorMatrix(vector<vector<T>> &matrix) {
|
||||
cout << "[" << '\n';
|
||||
for (vector<T> &list : matrix)
|
||||
@@ -74,7 +74,7 @@ template <typename T> void printVectorMatrix(vector<vector<T>> &matrix) {
|
||||
cout << "]" << '\n';
|
||||
}
|
||||
|
||||
/* Print linked list */
|
||||
/* Print a linked list */
|
||||
void printLinkedList(ListNode *head) {
|
||||
vector<int> list;
|
||||
while (head != nullptr) {
|
||||
@@ -104,7 +104,7 @@ void showTrunks(Trunk *p) {
|
||||
}
|
||||
|
||||
/**
|
||||
* 打印二叉树
|
||||
* Print a binary tree
|
||||
* This tree printer is borrowed from TECHIE DELIGHT
|
||||
* https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
*/
|
||||
@@ -139,12 +139,12 @@ void printTree(TreeNode *root, Trunk *prev, bool isRight) {
|
||||
printTree(root->left, &trunk, false);
|
||||
}
|
||||
|
||||
/* Print binary tree */
|
||||
/* Print a binary tree */
|
||||
void printTree(TreeNode *root) {
|
||||
printTree(root, nullptr, false);
|
||||
}
|
||||
|
||||
/* Print stack */
|
||||
/* Print the stack */
|
||||
template <typename T> void printStack(stack<T> stk) {
|
||||
// Reverse the input stack
|
||||
stack<T> tmp;
|
||||
@@ -166,7 +166,7 @@ template <typename T> void printStack(stack<T> stk) {
|
||||
cout << "[" + s.str() + "]" << '\n';
|
||||
}
|
||||
|
||||
/* Print queue */
|
||||
/* Print the queue */
|
||||
template <typename T> void printQueue(queue<T> queue) {
|
||||
// Generate the string to print
|
||||
ostringstream s;
|
||||
@@ -182,7 +182,7 @@ template <typename T> void printQueue(queue<T> queue) {
|
||||
cout << "[" + s.str() + "]" << '\n';
|
||||
}
|
||||
|
||||
/* Print deque */
|
||||
/* Print the deque */
|
||||
template <typename T> void printDeque(deque<T> deque) {
|
||||
// Generate the string to print
|
||||
ostringstream s;
|
||||
@@ -198,8 +198,8 @@ template <typename T> void printDeque(deque<T> deque) {
|
||||
cout << "[" + s.str() + "]" << '\n';
|
||||
}
|
||||
|
||||
/* Print hash table */
|
||||
// Define template parameters TKey and TValue to specify key-value pair types
|
||||
/* Print the hash table */
|
||||
// Define template parameters TKey and TValue for the key and value types
|
||||
template <typename TKey, typename TValue> void printHashMap(unordered_map<TKey, TValue> map) {
|
||||
for (auto kv : map) {
|
||||
cout << kv.first << " -> " << kv.second << '\n';
|
||||
@@ -216,12 +216,12 @@ template <typename T, typename S, typename C> S &Container(priority_queue<T, S,
|
||||
return HackedQueue::Container(pq);
|
||||
}
|
||||
|
||||
/* Print heap (priority queue) */
|
||||
/* Print the heap (priority queue) */
|
||||
template <typename T, typename S, typename C> void printHeap(priority_queue<T, S, C> &heap) {
|
||||
vector<T> vec = Container(heap);
|
||||
cout << "Heap array representation:";
|
||||
cout << "Array representation of the heap:";
|
||||
printVector(vec);
|
||||
cout << "Heap tree representation:" << endl;
|
||||
cout << "Tree representation of the heap:" << endl;
|
||||
TreeNode *root = vectorToTree(vec);
|
||||
printTree(root);
|
||||
freeMemoryTree(root);
|
||||
|
||||
@@ -23,11 +23,11 @@ struct TreeNode {
|
||||
}
|
||||
};
|
||||
|
||||
// For the serialization encoding rules, please refer to:
|
||||
// https://www.hello-algo.com/chapter_tree/array_representation_of_tree/
|
||||
// Array representation of binary tree:
|
||||
// For the serialization rules, see:
|
||||
// https://www.hello-algo.com/en/chapter_tree/array_representation_of_tree/
|
||||
// Array representation of the binary tree:
|
||||
// [1, 2, 3, 4, None, 6, 7, 8, 9, None, None, 12, None, None, 15]
|
||||
// Linked list representation of binary tree:
|
||||
// Linked representation of the binary tree:
|
||||
// /——— 15
|
||||
// /——— 7
|
||||
// /——— 3
|
||||
@@ -74,7 +74,7 @@ vector<int> treeToVecor(TreeNode *root) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Free binary tree memory */
|
||||
/* Free the binary tree memory */
|
||||
void freeMemoryTree(TreeNode *root) {
|
||||
if (root == nullptr)
|
||||
return;
|
||||
|
||||
@@ -17,7 +17,7 @@ struct Vertex {
|
||||
}
|
||||
};
|
||||
|
||||
/* Input value list vals, return vertex list vets */
|
||||
/* Given the list of values vals, return the list of vertices vets */
|
||||
vector<Vertex *> valsToVets(vector<int> vals) {
|
||||
vector<Vertex *> vets;
|
||||
for (int val : vals) {
|
||||
@@ -26,7 +26,7 @@ vector<Vertex *> valsToVets(vector<int> vals) {
|
||||
return vets;
|
||||
}
|
||||
|
||||
/* Input vertex list vets, return value list vals */
|
||||
/* Given the list of vertices vets, return the list of values vals */
|
||||
vector<int> vetsToVals(vector<Vertex *> vets) {
|
||||
vector<int> vals;
|
||||
for (Vertex *vet : vets) {
|
||||
|
||||
Reference in New Issue
Block a user