Skip to content

DoLovya/LeetcodeLocalDebug-CPP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LeetCode 本地调试环境

这是一个用于本地方便调试 LeetCode 代码的 C++ 环境,提供了常用的数据结构和工具函数,让你可以在本地快速测试和调试 LeetCode 题目的解决方案。

数据结构工具使用说明

1. 链表工具 (leetcode_list.h)

使用方法

#include "leetcode_list.h"

// 从控制台输入创建链表
// 输入格式:先输入节点数量,然后输入各节点值
// 例如:5 1 2 3 4 5
ListNode* head = input_list();

// 输出链表
print_list(head);

2. 二叉树工具 (leetcode_tree.h)

使用方法

#include "leetcode_tree.h"

// 从字符串构建二叉树(LeetCode格式)
// 输入格式:[1,2,3,null,null,4,5]
string input = "[1,2,3,null,null,4,5]";
TreeNode* root = build_tree(input);

// 从控制台输入构建二叉树
TreeNode* root = input_tree();

// 打印二叉树(前序遍历)
print_tree(root);

3. 向量工具 (leetcode_vector.h)

使用方法

#include "leetcode_vector.h"

// 打印一维向量
vector<int> nums = {1, 2, 3, 4, 5};
print_vector(nums);

// 打印二维向量
vector<vector<int>> matrix = {{1, 2}, {3, 4}, {5, 6}};
print_2d_vector(matrix);

// 从字符串创建整数向量
// 输入格式:[1,2,3,4,5]
string str = "[1,2,3,4,5]";
vector<int> vec = string_to_int_vector(str);

// 从控制台输入创建整数向量
vector<int> vec = input_int_vector();

// 从字符串创建字符串向量
// 输入格式:["MyQueue","push","push","peek","pop","empty"]
string str = "[\"MyQueue\",\"push\",\"push\",\"peek\",\"pop\",\"empty\"]";
vector<string> vec = string_to_string_vector(str);

// 从控制台输入创建字符串向量
vector<string> vec = input_string_vector();

string str = "[[],[1],[2],[],[],[]]";
vector<vector<int>> vec2d = string_to_2d_int_vector(str);

vector<vector<int>> vec2d = input_2d_int_vector();

string str = "[[\"1\",\"1\",\"1\",\"1\",\"0\"],[\"1\",\"1\",\"0\",\"1\",\"0\"]]";
vector<vector<char>> grid = string_to_2d_char_vector(str);
print_2d_char_vector(grid);

vector<vector<char>> grid = input_2d_char_vector();

4. Solution 模板 (leetcode_test.cpp)

提供了标准的 Solution 类模板,你可以直接在这里编写你的解决方案。

5. 自定义数据结构测试

对于需要测试自定义数据结构(如队列、栈等)的 LeetCode 题目,可以使用以下模式:

#include "leetcode_headers.h"
using namespace std;

class MyQueue {
public:
    MyQueue() {}
    void push(int x) { /* implementation */ }
    int pop() { /* implementation */ return 0; }
    int peek() { /* implementation */ return 0; }
    bool empty() { /* implementation */ return true; }
};

int main() {
    auto operations = input_string_vector();
    auto values = input_2d_int_vector();

    MyQueue* my_queue = nullptr;
    for (int i = 0; i < operations.size(); i++) {
        auto operation = operations[i];
        if (operation == "MyQueue") {
            my_queue = new MyQueue();
            std::cout << "null" << " ";
        }
        if (operation == "push") {
            my_queue->push(values[i][0]);
            std::cout << "null" << " ";
        }
        if (operation == "peek") {
            std::cout << my_queue->peek() << " ";
        }
        if (operation == "pop") {
            std::cout << my_queue->pop() << " ";
        }
        if (operation == "empty") {
            std::cout << my_queue->empty() << " ";
        }
    }
    return 0;
}

输入示例:

  • 操作序列:["MyQueue","push","push","peek","pop","empty"]
  • 参数值:[[],[1],[2],[],[],[]]

项目结构

├── .gitignore
├── CMakeLists.txt
├── LICENSE
├── README.md
├── include\
│   ├── leetcode_headers.h
│   ├── leetcode_list.h
│   ├── leetcode_tree.h
│   └── leetcode_vector.h
└── src\
    └── leetcode_test.cpp

使用方法

环境要求

  • CMake 3.10 或更高版本
  • C++17 编译器

构建项目

  1. 在项目根目录创建构建目录:
mkdir build
cd build
  1. 生成构建文件:
cmake ..
  1. 编译项目:
cmake --build .

使用步骤

  1. 编写解决方案:在 src/leetcode_test.cpp 文件中的 Solution 类里编写你的解决方案

  2. 修改主函数:根据题目要求,在 src/leetcode_test.cpp 中修改输入输出逻辑

  3. 编译运行:使用上述构建步骤编译并运行程序

示例使用

当前项目包含了一个最长回文子串的示例实现:

// 在 src/leetcode_test.cpp 中
#include "leetcode_headers.h"
using namespace std;

class Solution {
public:
    string longestPalindrome(string s) {
        // 你的实现代码
    }
};

int main() {
    std::string str;
    std::cin >> str;

    Solution s;
    auto result = s.longestPalindrome(str);
    std::cout << result << std::endl;
    return 0;
}

自定义使用

你可以根据不同的题目需求:

  1. 修改输入输出:在 src/leetcode_test.cpp 中根据题目要求修改输入输出格式
  2. 使用工具函数:利用提供的链表和向量工具函数简化数据处理
  3. 添加新的工具函数:根据需要在相应的头文件中添加新的工具函数

注意事项

  • 确保在提交 LeetCode 时只复制 Solution 类中的核心代码
  • 本地调试时可以使用所有提供的工具函数
  • 项目使用 C++17 标准,确保你的编译器支持

贡献

欢迎提交 Issue 和 Pull Request 来改进这个调试环境!

许可证

本项目采用 MIT 许可证。

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published