-
Notifications
You must be signed in to change notification settings - Fork 12.4k
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
Showing
112 changed files
with
6,994 additions
and
10 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
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,10 @@ | ||
# Ignore all | ||
* | ||
# Unignore all with extensions | ||
!*.* | ||
# Unignore all dirs | ||
!*/ | ||
|
||
*.dSYM/ | ||
|
||
build/ |
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,20 @@ | ||
cmake_minimum_required(VERSION 3.10) | ||
project(hello_algo CXX) | ||
|
||
set(CMAKE_CXX_STANDARD 11) | ||
|
||
include_directories(./include) | ||
|
||
add_subdirectory(chapter_computational_complexity) | ||
add_subdirectory(chapter_array_and_linkedlist) | ||
add_subdirectory(chapter_stack_and_queue) | ||
add_subdirectory(chapter_hashing) | ||
add_subdirectory(chapter_tree) | ||
add_subdirectory(chapter_heap) | ||
add_subdirectory(chapter_graph) | ||
add_subdirectory(chapter_searching) | ||
add_subdirectory(chapter_sorting) | ||
add_subdirectory(chapter_divide_and_conquer) | ||
add_subdirectory(chapter_backtracking) | ||
add_subdirectory(chapter_dynamic_programming) | ||
add_subdirectory(chapter_greedy) |
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,4 @@ | ||
add_executable(array array.cpp) | ||
add_executable(linked_list linked_list.cpp) | ||
add_executable(list list.cpp) | ||
add_executable(my_list my_list.cpp) |
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,113 @@ | ||
/** | ||
* File: array.cpp | ||
* Created Time: 2022-11-25 | ||
* Author: krahets ([email protected]) | ||
*/ | ||
|
||
#include "../utils/common.hpp" | ||
|
||
/* Random access to elements */ | ||
int randomAccess(int *nums, int size) { | ||
// Randomly select a number in the range [0, size) | ||
int randomIndex = rand() % size; | ||
// Retrieve and return a random element | ||
int randomNum = nums[randomIndex]; | ||
return randomNum; | ||
} | ||
|
||
/* Extend array length */ | ||
int *extend(int *nums, int size, int enlarge) { | ||
// Initialize an extended length array | ||
int *res = new int[size + enlarge]; | ||
// Copy all elements from the original array to the new array | ||
for (int i = 0; i < size; i++) { | ||
res[i] = nums[i]; | ||
} | ||
// Free memory | ||
delete[] nums; | ||
// Return the new array after expansion | ||
return res; | ||
} | ||
|
||
/* Insert element num at `index` */ | ||
void insert(int *nums, int size, int num, int index) { | ||
// Move all elements after `index` one position backward | ||
for (int i = size - 1; i > index; i--) { | ||
nums[i] = nums[i - 1]; | ||
} | ||
// Assign num to the element at index | ||
nums[index] = num; | ||
} | ||
|
||
/* Remove the element at `index` */ | ||
void remove(int *nums, int size, int index) { | ||
// Move all elements after `index` one position forward | ||
for (int i = index; i < size - 1; i++) { | ||
nums[i] = nums[i + 1]; | ||
} | ||
} | ||
|
||
/* Traverse array */ | ||
void traverse(int *nums, int size) { | ||
int count = 0; | ||
// Traverse array by index | ||
for (int i = 0; i < size; i++) { | ||
count += nums[i]; | ||
} | ||
} | ||
|
||
/* Search for a specified element in the array */ | ||
int find(int *nums, int size, int target) { | ||
for (int i = 0; i < size; i++) { | ||
if (nums[i] == target) | ||
return i; | ||
} | ||
return -1; | ||
} | ||
|
||
/* Driver Code */ | ||
int main() { | ||
/* Initialize an array */ | ||
int size = 5; | ||
int *arr = new int[size]; | ||
cout << "Array arr = "; | ||
printArray(arr, size); | ||
|
||
int *nums = new int[size]{1, 3, 2, 5, 4}; | ||
cout << "Array nums = "; | ||
printArray(nums, size); | ||
|
||
/* Random access */ | ||
int randomNum = randomAccess(nums, size); | ||
cout << "Get a random element from nums = " << randomNum << endl; | ||
|
||
/* Length extension */ | ||
int enlarge = 3; | ||
nums = extend(nums, size, enlarge); | ||
size += enlarge; | ||
cout << "Extend the array length to 8, resulting in nums = "; | ||
printArray(nums, size); | ||
|
||
/* Insert element */ | ||
insert(nums, size, 6, 3); | ||
cout << "Insert the number 6 at index 3, resulting in nums = "; | ||
printArray(nums, size); | ||
|
||
/* Remove element */ | ||
remove(nums, size, 2); | ||
cout << "Remove the element at index 2, resulting in nums = "; | ||
printArray(nums, size); | ||
|
||
/* Traverse array */ | ||
traverse(nums, size); | ||
|
||
/* Search for elements */ | ||
int index = find(nums, size, 3); | ||
cout << "Find element 3 in nums, index = " << index << endl; | ||
|
||
// Free memory | ||
delete[] arr; | ||
delete[] nums; | ||
|
||
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,89 @@ | ||
/** | ||
* File: linked_list.cpp | ||
* Created Time: 2022-11-25 | ||
* Author: krahets ([email protected]) | ||
*/ | ||
|
||
#include "../utils/common.hpp" | ||
|
||
/* Insert node P after node n0 in the linked list */ | ||
void insert(ListNode *n0, ListNode *P) { | ||
ListNode *n1 = n0->next; | ||
P->next = n1; | ||
n0->next = P; | ||
} | ||
|
||
/* Remove the first node after node n0 in the linked list */ | ||
void remove(ListNode *n0) { | ||
if (n0->next == nullptr) | ||
return; | ||
// n0 -> P -> n1 | ||
ListNode *P = n0->next; | ||
ListNode *n1 = P->next; | ||
n0->next = n1; | ||
// Free memory | ||
delete P; | ||
} | ||
|
||
/* Access the node at `index` in the linked list */ | ||
ListNode *access(ListNode *head, int index) { | ||
for (int i = 0; i < index; i++) { | ||
if (head == nullptr) | ||
return nullptr; | ||
head = head->next; | ||
} | ||
return head; | ||
} | ||
|
||
/* Search for the first node with value target in the linked list */ | ||
int find(ListNode *head, int target) { | ||
int index = 0; | ||
while (head != nullptr) { | ||
if (head->val == target) | ||
return index; | ||
head = head->next; | ||
index++; | ||
} | ||
return -1; | ||
} | ||
|
||
/* Driver Code */ | ||
int main() { | ||
/* Initialize linked list */ | ||
// Initialize each node | ||
ListNode *n0 = new ListNode(1); | ||
ListNode *n1 = new ListNode(3); | ||
ListNode *n2 = new ListNode(2); | ||
ListNode *n3 = new ListNode(5); | ||
ListNode *n4 = new ListNode(4); | ||
// Build references between nodes | ||
n0->next = n1; | ||
n1->next = n2; | ||
n2->next = n3; | ||
n3->next = n4; | ||
cout << "The initialized linked list is" << endl; | ||
printLinkedList(n0); | ||
|
||
/* Insert node */ | ||
insert(n0, new ListNode(0)); | ||
cout << "Linked list after inserting the node is" << endl; | ||
printLinkedList(n0); | ||
|
||
/* Remove node */ | ||
remove(n0); | ||
cout << "Linked list after removing the node is" << endl; | ||
printLinkedList(n0); | ||
|
||
/* Access node */ | ||
ListNode *node = access(n0, 3); | ||
cout << "The value of the node at index 3 in the linked list = " << node->val << endl; | ||
|
||
/* Search node */ | ||
int index = find(n0, 2); | ||
cout << "The index of the node with value 2 in the linked list = " << index << endl; | ||
|
||
// Free memory | ||
freeMemoryLinkedList(n0); | ||
|
||
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,72 @@ | ||
/** | ||
* File: list.cpp | ||
* Created Time: 2022-11-25 | ||
* Author: krahets ([email protected]) | ||
*/ | ||
|
||
#include "../utils/common.hpp" | ||
|
||
/* Driver Code */ | ||
int main() { | ||
/* Initialize list */ | ||
vector<int> nums = {1, 3, 2, 5, 4}; | ||
cout << "List nums = "; | ||
printVector(nums); | ||
|
||
/* Access element */ | ||
int num = nums[1]; | ||
cout << "Access the element at index 1, obtained num = " << num << endl; | ||
|
||
/* Update element */ | ||
nums[1] = 0; | ||
cout << "Update the element at index 1 to 0, resulting in nums = "; | ||
printVector(nums); | ||
|
||
/* Clear list */ | ||
nums.clear(); | ||
cout << "After clearing the list, nums = "; | ||
printVector(nums); | ||
|
||
/* Add element at the end */ | ||
nums.push_back(1); | ||
nums.push_back(3); | ||
nums.push_back(2); | ||
nums.push_back(5); | ||
nums.push_back(4); | ||
cout << "After adding elements, nums = "; | ||
printVector(nums); | ||
|
||
/* Insert element in the middle */ | ||
nums.insert(nums.begin() + 3, 6); | ||
cout << "Insert the number 6 at index 3, resulting in nums = "; | ||
printVector(nums); | ||
|
||
/* Remove element */ | ||
nums.erase(nums.begin() + 3); | ||
cout << "Remove the element at index 3, resulting in nums = "; | ||
printVector(nums); | ||
|
||
/* Traverse the list by index */ | ||
int count = 0; | ||
for (int i = 0; i < nums.size(); i++) { | ||
count += nums[i]; | ||
} | ||
/* Traverse the list elements */ | ||
count = 0; | ||
for (int x : nums) { | ||
count += x; | ||
} | ||
|
||
/* Concatenate two lists */ | ||
vector<int> nums1 = {6, 8, 7, 10, 9}; | ||
nums.insert(nums.end(), nums1.begin(), nums1.end()); | ||
cout << "Concatenate list nums1 to nums, resulting in nums = "; | ||
printVector(nums); | ||
|
||
/* Sort list */ | ||
sort(nums.begin(), nums.end()); | ||
cout << "After sorting the list, nums = "; | ||
printVector(nums); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.