使用clion刷leetcode

如何优雅的使用clion刷leetcode

安装插件:LeetCode Editor)

插件配置:

这样我们每打开一个项目,就会创建类似的文件

我们的项目结构:

我们在题解文件中导入头文件myHeader.h并将新建的文件添加到cmakelists.txt文件,这样就不会报错了

  • myHeader.h
c++ 复制代码
#ifndef MY_HEADER_H
#define MY_HEADER_H

#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <stack>
#include <queue>
#include <deque>
#include <list>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstddef>
#include <cassert>
#include <numeric>
#include <functional>
#include <sstream>
#include <iterator>
#include <bitset>
#include <iomanip>
#include <memory>
#include <tuple>
#include <array>
#include <stdexcept>
#include <fstream>
#include <regex>
#include <random>
#include <chrono>
#include <initializer_list>
#include <utility>

using namespace std;

// 重载 << 运算符用于 std::vector
template<typename T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &container) {
    os << "[";
    for (auto it = container.begin(); it != container.end(); ++it) {
        os << *it;
        if (std::next(it) != container.end()) {
            os << ", ";
        }
    }
    os << "]";
    return os;
}

// 重载 << 运算符用于 std::queue
template<typename T>
std::ostream &operator<<(std::ostream &os, std::queue<T> container) {
    os << "[";
    while (!container.empty()) {
        os << container.front();
        container.pop();
        if (!container.empty()) {
            os << ", ";
        }
    }
    os << "]";
    return os;
}

// 重载 << 运算符用于 std::deque
template<typename T>
std::ostream &operator<<(std::ostream &os, const std::deque<T> &container) {
    os << "[";
    for (auto it = container.begin(); it != container.end(); ++it) {
        os << *it;
        if (std::next(it) != container.end()) {
            os << ", ";
        }
    }
    os << "]";
    return os;
}

// 重载 << 运算符用于 std::list
template<typename T>
std::ostream &operator<<(std::ostream &os, const std::list<T> &container) {
    os << "[";
    for (auto it = container.begin(); it != container.end(); ++it) {
        os << *it;
        if (std::next(it) != container.end()) {
            os << ", ";
        }
    }
    os << "]";
    return os;
}

// 重载 << 运算符用于 std::set
template<typename T>
std::ostream &operator<<(std::ostream &os, const std::set<T> &container) {
    os << "[";
    for (auto it = container.begin(); it != container.end(); ++it) {
        os << *it;
        if (std::next(it) != container.end()) {
            os << ", ";
        }
    }
    os << "]";
    return os;
}

// 重载 << 运算符用于 std::unordered_set
template<typename T>
std::ostream &operator<<(std::ostream &os, const std::unordered_set<T> &container) {
    os << "[";
    for (auto it = container.begin(); it != container.end(); ++it) {
        os << *it;
        if (std::next(it) != container.end()) {
            os << ", ";
        }
    }
    os << "]";
    return os;
}

// 重载 << 运算符用于 std::map
template<typename K, typename V>
std::ostream &operator<<(std::ostream &os, const std::map<K, V> &container) {
    os << "{";
    for (auto it = container.begin(); it != container.end(); ++it) {
        os << it->first << ": " << it->second;
        if (std::next(it) != container.end()) {
            os << ", ";
        }
    }
    os << "}";
    return os;
}

// 重载 << 运算符用于 std::unordered_map
template<typename K, typename V>
std::ostream &operator<<(std::ostream &os, const std::unordered_map<K, V> &container) {
    os << "{";
    for (auto it = container.begin(); it != container.end(); ++it) {
        os << it->first << ": " << it->second;
        if (std::next(it) != container.end()) {
            os << ", ";
        }
    }
    os << "}";
    return os;
}

// 重载 << 运算符用于 std::pair
template<typename T1, typename T2>
std::ostream &operator<<(std::ostream &os, const std::pair<T1, T2> &p) {
    os << "(" << p.first << ", " << p.second << ")";
    return os;
}

// 重载 << 运算符用于 std::stack
template<typename T>
std::ostream &operator<<(std::ostream &os, std::stack<T> container) {
    os << "[";
    while (!container.empty()) {
        os << container.top();
        container.pop();
        if (!container.empty()) {
            os << ", ";
        }
    }
    os << "]";
    return os;
}

// 重载 << 运算符用于 std::priority_queue
template<typename T>
std::ostream &operator<<(std::ostream &os, std::priority_queue<T> container) {
    os << "[";
    while (!container.empty()) {
        os << container.top();
        container.pop();
        if (!container.empty()) {
            os << ", ";
        }
    }
    os << "]";
    return os;
}


#endif // MY_HEADER_H
  • CMakeLists.txt
cmake 复制代码
cmake_minimum_required(VERSION 3.28)
project(LeetCodeTime)

set(CMAKE_CXX_STANDARD 17)

# 添加源文件,选择运行的题解
file(GLOB SRC_FILES ${PROJECT_SOURCE_DIR}/leetcode/editor/en/twoSum.cpp)

add_executable(LeetCodeTime main.cpp ${SRC_FILES})

target_include_directories(LeetCodeTime PUBLIC ${PROJECT_SOURCE_DIR}/include)

修改file(GLOB SRC_FILES ${PROJECT_SOURCE_DIR}/leetcode/editor/en/twoSum.cpp),这样就能调试了~

  • main.cpp
c++ 复制代码
#include <iostream>
void runProblem();
int main() {
    system("chcp 65001"); // 支持中文
    std::cout << "50000个测试用例开始测试!" << std::endl;
    runProblem();
    return 0;
}
  • twoSum.cpp
c++ 复制代码
// 1 Two Sum 2024-07-09 19:44:32
#include "myHeader.h"
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        for (int i = 0; i < nums.size(); i++) {
            for (int j = i + 1; j < nums.size(); j++) {
                if (nums[j] == target - nums[i]) {
                    return {i, j};
                }
            }
        }
        return {};
    }
};
//leetcode submit region end(Prohibit modification and deletion)

void runProblem() {
    Solution solution;
    vector<int> nums = {2, 7, 11, 15};
    int target = 9;
    vector<int> result = solution.twoSum(nums, target);
    for (int num : result) {
        cout << num << " ";
    }
    cout << endl;
}

例如,我们要调试两数之和这道题,我们就需要实现runProblem,自行设置测试用例,这样通过打断点就能调试了~,注意,这里的runProblem方法是最简单的方法,必要情况下,你可以完善如下功能,创建一个生成测试用例的函数,同时输出与正确答案结果不同的测试用例,至于正确结果,可以直接拿题解的就行。例如如下示例

c++ 复制代码
// 1 Two Sum 2024-07-09 19:44:32
#include "myHeader.h"
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public:
    vector<int> twoSum(vector<int> &nums, int target) {
        for (int i = 0; i < nums.size(); i++) {
            for (int j = i + 1; j < nums.size(); j++) {
                if (nums[j] == target - nums[i]) {
                    return {i, j};
                }
            }
        }
        return {};
    }
};
//leetcode submit region end(Prohibit modification and deletion)
// 生成随机数组
vector<int> generateRandomArray(int maxSize, int maxValue) {
    vector<int> randomArray;
    // 创建随机数生成器
    std::random_device rd;
    // 创建一个指定随机数范围的随机数引擎
    std::mt19937 eng(rd());
    std::uniform_int_distribution<int> maxValueEng(0, maxValue);
    std::uniform_int_distribution<int> maxSizeEng(0, maxSize);

    // 生成随机数组
    maxSize = maxSizeEng(eng);
    randomArray.reserve(maxSize);
    for (int i = 0; i < maxSize; ++i) {
        // 数组内没有重复元素
        int randomValue = maxValueEng(eng);
        while (find(randomArray.begin(), randomArray.end(), randomValue) != randomArray.end()) {
            randomValue = maxValueEng(eng);
        }
    }
    return randomArray;
}

// 获取随机值
int getRandomValue(int maxValue) {
    std::random_device rd;
    std::mt19937 eng(rd());
    std::uniform_int_distribution<int> maxValueEng(0, 2 * maxValue);
    return maxValueEng(eng);
}

// 比较器
vector<int> comparator(vector<int> &nums, int target) {
    unordered_map<int, int> numMap;
    int n = nums.size();
    for (int i = 0; i < n; i++) {
        int complement = target - nums[i];
        if (numMap.count(complement)) {
            return {numMap[complement], i};
        }
        numMap[nums[i]] = i;
    }
    return {}; // No solution found
}
// 比较两个数组是否相等
bool isEqual(vector<int> &nums1, vector<int> &nums2) {
    return nums1 == nums2;
}
// 测试
void runProblem() {
    int testTime = 50000; // 测试次数
    int maxSize = 50;    // 数组最大长度
    int maxValue = 10000;   // 数组元素最大值
    bool succeed = true;
    Solution solution;
    for (int i = 0; i < testTime; ++i) {
        vector<int> randomArray = generateRandomArray(maxSize, maxValue);
        int target = getRandomValue(maxValue);
        vector<int> solutionResult = solution.twoSum(randomArray, target);
        vector<int> comparatorResult = comparator(randomArray, target);
        if(!isEqual(solutionResult, comparatorResult)) {
            cout << "第" << i << "次测试失败!" << endl;
            cout << "原数组:" << randomArray << endl;
            cout << "目标值:" << target << endl;
            cout << "Solution result: " << solutionResult << endl;
            cout << "Comparator result: " << comparatorResult << endl;
            succeed = false;
            break;
        }
    }
    cout << (succeed ? "测试用例全部通过!" : "测试失败!") << endl;
}
相关推荐
秋夫人3 分钟前
B+树(B+TREE)索引
数据结构·算法
梦想科研社39 分钟前
【无人机设计与控制】四旋翼无人机俯仰姿态保持模糊PID控制(带说明报告)
开发语言·算法·数学建模·matlab·无人机
Milo_K40 分钟前
今日 leetCode 15.三数之和
算法·leetcode
Darling_0044 分钟前
LeetCode_sql_day28(1767.寻找没有被执行的任务对)
sql·算法·leetcode
AlexMercer10121 小时前
【C++】二、数据类型 (同C)
c语言·开发语言·数据结构·c++·笔记·算法
Greyplayground1 小时前
【算法基础实验】图论-BellmanFord最短路径
算法·图论·最短路径
蓑 羽1 小时前
力扣438 找到字符串中所有字母异位词 Java版本
java·算法·leetcode
源代码:趴菜1 小时前
LeetCode63:不同路径II
算法·leetcode·职场和发展
儿创社ErChaungClub1 小时前
解锁编程新境界:GitHub Copilot 让效率翻倍
人工智能·算法
前端西瓜哥1 小时前
贝塞尔曲线算法:求贝塞尔曲线和直线的交点
前端·算法