Skip to content

About

My collection of algorithm and data structure solutions.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

Algorithms-Cpp

My collection of algorithm and data structure solutions.

Get Started

  1. (Optional)Install vcpkg :
    1. git clone https://github.com/microsoft/vcpkg.git
    2. cd vcpkg
    3. .\bootstrap-vcpkg.bat
    4. add install path to system environment variable VCPKG_ROOT : set VCPKG_ROOT="C:\path\to\vcpkg"; set PATH=%VCPKG_ROOT%;%PATH%
  2. Install Xmake : winget install xmake
  3. Develop with VS:
    1. Run xmake project -k vsxmake -m "debug,release"
    2. Open the generated VS solution file in vsxmake20xx folder. Exercise projects are grouped by their source path, such as Algorithms/binary_search, and source, header, metadata, and test files appear directly beneath their target.
    3. Choose Release/Debug x64 and Build in Visual Studio.
  4. Develop with VSCode:
    1. (Optional) Set Debug/Release mode: xmake config -m [debug|release] -y
    2. (Optional) Make compile_commands.json file: xmake project -k compile_commands .vscode
    3. Open vscode with Developer Command Prompt for VSxx
  5. Run an Exercise target:
    1. Build: xmake build xxx
    2. Run: xmake run xxx [-d]

Exercise Workflow

Each Exercise is built as an independent Catch2 test executable. Xmake manages Catch2 3.15.x, discovers Exercise directories recursively, and creates one target for every valid exercise.json plus *_test.cpp pair. Every Exercise depends on the basic support library and is excluded from the default build.

  • Create a Problem Exercise while retaining the problem's native public API:
# Simple
xmake new --kind=problem --root=algorithms/sort --id=LC215 --slug=kth_largest_element
# Full
xmake new --kind=problem --root=algorithms/sort --id=LC215 --slug=kth_largest_element `
  --name="Kth Largest Element" --difficulty=Medium `
  --tip="Quick select" --url=https://leetcode.com/problems/kth-largest-element-in-an-array/

This maps to directory and target lc215_kth_largest_element, namespace AlgoCpp::Problem::Algorithm::LC215, and Catch2 tags [problem][algorithm][sort][lc215].

  • Create a reusable Template Exercise without an external problem ID:
xmake new --kind=template --root=data_structures/array --slug=dynamic_array

This maps to directory and target ds_dynamic_array, namespace AlgoCpp::Template::DataStructure::DynamicArray, and tags [template][data_structure][array][dynamic_array]. The initial scaffold contains commented interface examples and a failing placeholder test; replace that placeholder with real assertions after choosing the Exercise's natural API.

  • The top-level root determines the namespace domain: algorithms → Algorithm, data_structures → DataStructure, and reviews → Review. Problem IDs and slugs are safely normalized and reported. Unsafe input, an unsupported root, an existing target, or a duplicate Problem ID fails without leaving a partial Exercise. --url is optional for both Problem and Template Exercises; when supplied, it is stored in metadata and emitted once as // refs : URL above the generated header.

A typical Exercise is:

lc215_kth_largest_element/
├── kth_largest_element.h
├── kth_largest_element.cpp
├── kth_largest_element_test.cpp
├── duplicates_test.cpp          # optional; any number of *_test.cpp files
└── exercise.json

Use the same Catch2 TEST_CASE form for Huawei, LeetCode, and Template Exercises; invoke each Exercise through its natural public API:

TEST_CASE("LC215 finds the kth largest element", "[problem][algorithm][sort][lc215]")
{
    Solution solution;
    std::vector<int> nums{3, 2, 1, 5, 6, 4};
    REQUIRE(solution.findKthLargest(nums, 2) == 5);
}
TEST_CASE("HJ02 counts characters without case sensitivity", "[problem][review][huawei][hj02]")
{
    REQUIRE(CountChar("ABCabc", 'A') == 2);
}

Run one Exercise or every Exercise Test:

xmake run lc215_kth_largest_element
xmake test

exercise.json is the catalog source of truth. Regenerate the managed README region with xmake index, or verify that it is current without writing via xmake index --check. The index follows the algorithms and data_structures directory categories; Review Exercises remain buildable but are intentionally omitted.

Exercise Index

Algorithms

Binary Search

ID Name Difficulty Tip
Algorithm.BinarySearch Binary Search
LC34 Find First and Last Position of Element in Sorted Array Medium 查找区间
LC69 Sqrt(x) Easy 开方
LC81 Search In Rotated Sorted Array II Medium 旋转数组

Divide Conquer

ID Name Difficulty Tip
LC241 Different Ways to Add Parentheses

Dynamic Programming

ID Name Difficulty Tip
LC10 Regular Expression Matching Hard 字符串编辑问题
LC64 Minimum Path Sum Medium 二维
LC70 Climbing Stairs Easy 一维
LC72 Edit Distance Hard 字符串编辑问题
LC91 Decode Ways Medium 分割问题
LC121 Best Time to Buy and Sell Stock Easy 股票交易问题
LC139 Word Break Medium 分割问题
LC188 Best Time to Buy and Sell Stock IV Hard 股票交易问题
LC198 Hourse Robber Easy 一维
LC221 Maximal Square Medium 二维
LC279 Perfect Square Medium 分割问题
LC300 Logest Increasing Subsequence Medium 子序列问题
LC309 Best Time to Buy and Sell Stock with Cooldown Medium 股票交易问题
LC322 Coin Change Medium 完全背包问题
LC413 Arithmetic Slices Medium 一维
LC416 Partition Equal Subset Sum Medium 背包问题
LC474 Ones and Zeros Medium 0-1背包问题
LC542 0-1 Matrix Medium 二维
LC650 2 Keys Keyboard Medium 字符串编辑问题
LC1143 Logest Common Subsequence Medium 子序列问题

Greedy Algorithm

ID Name Difficulty Tip
LC135 Candy Hard 分配问题
LC435 Non-Overlapping Intervals Medium 区间问题
LC455 Assign Cookies Easy 分配问题
LC946 Validate Stack Sequences 栈
LCOF45 Arrange Array to Minimum Number Easy 排序

Search

ID Name Difficulty Tip
LC46 Permutations Medium 回溯法
LC51 N-Queens Hard 回溯法
LC77 Combinations Medium 回溯法
LC79 Word Search Medium 回溯法
LC126 Word Ladder II Hard 广度优先搜索
LC417 Pacific Atlantic Waterflow Medium 深度优先搜索
LC494 Target Sum
LC547 Number of Provinces Medium 深度优先搜索
LC695 Max Area of Island Medium 深度优先搜索
LC752 Open The Lock
LC934 Shortest Bridge Medium 广度优先搜索
LC1091 Shortest Path In Binary Matrix
LC1129 Shortest Path with Alternating Colors
LC1254 Number Of Closed Islands

Sort

ID Name Difficulty Tip
LC56 Merge Intervals Medium
LC215 Kth Largest Element Medium 快速选择
LC347 Top k Frequent Elements Medium 桶排序

Two Pointers

ID Name Difficulty Tip
LC3 Longest Substring Without Repeating Characters
LC16 3sum Closest
LC27 Remove Element Easy 双指针
LC30 Substring With Concatenation Of All Words
LC76 Minimum Window Substring Hard 滑动窗口
LC88 Merge Sorted Array Easy
LC142 Link List Cycle II Medium 快慢指针
LC167 Two Sum II Easy

Data Structures

Array

ID Name Difficulty Tip
DataStructure.DynamicArray Dynamic Array
DataStructure.StaticArray Static Array

Graph

ID Name Difficulty Tip
LC210 Course Schedule Ii
LC444 Sequence Reconstruction
LC785 Is Graph Bipartite

Linked List

ID Name Difficulty Tip
LC21 Merge Two Sorted Lists
LC24 Swap Nodes In Pairs
LC61 Rotate List
LC86 Partition List
LC160 Intersection Of Two Linked List
LC206 Reverse Linked List
LC234 Palindrome Linked List
LC641 Design Circular Deque

Other

ID Name Difficulty Tip
LC146 Lru Cache
LC303 Range Sum Query
LC304 Range Sum Query 2d
LC503 Next Greater Element Ii
LC560 Subarray Sum Equals K
LC684 Redundant Connection
LC729 My Calendar I
LC739 Daily Temperatures
LC1109 Corporate Flight Bookings

Queue

ID Name Difficulty Tip
LC232 Implement Queue Using Stacks Easy

Tree

ID Name Difficulty Tip
LC98 Validate Binary Search Tree
LC99 Recover Binary Search Tree
LC101 Symmetric Tree
LC102 Binary Tree Level Order Traversal
LC104 Maximum Depth Of Binary Tree
LC105 Construct Binary Tree
LC110 Balanced Binary Tree
LC112 Path Sum
LC116 Populating Next Right Pointers In Each Node
LC117 Populating Next Right Pointers In Each Node Ii
LC144 Binary Tree Preorder Traversal
LC208 Implement Trie
LC437 Path Sum Iii
LC543 Diameter Of Binary Tree
LC637 Average Of Levels In Binary Tree
LC669 Trim A Binary Search Tree
LC1110 Delete Nodes And Return Forest

Reference

About

My collection of algorithm and data structure solutions.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages