使用clion刷leetcode

如何优雅的使用clion刷leetcode

安装插件:LeetCode Editor)

插件配置:

image-20240709210433380

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

image-20240709210536041

我们的项目结构:

image-20240709210129069

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

  • myHeader.h
#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_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
#include <iostream>
void runProblem();
int main() {
    system("chcp 65001"); // 支持中文
    std::cout << "50000个测试用例开始测试!" << std::endl;
    runProblem();
    return 0;
}

  • twoSum.cpp
// 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方法是最简单的方法,必要情况下,你可以完善如下功能,创建一个生成测试用例的函数,同时输出与正确答案结果不同的测试用例,至于正确结果,可以直接拿题解的就行。例如如下示例

// 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;
}

相关推荐

最近更新

  1. 基带成型(脉冲成形)

    2024-07-11 02:04:05       0 阅读
  2. mysql索引篇

    2024-07-11 02:04:05       0 阅读
  3. FedAvg

    FedAvg

    2024-07-11 02:04:05      0 阅读
  4. 我在vue2重生之vite加强版

    2024-07-11 02:04:05       0 阅读
  5. 2024.7.20 暑期训练记录(6)

    2024-07-11 02:04:05       0 阅读
  6. 调用第三方接口-OkHttpClient

    2024-07-11 02:04:05       0 阅读
  7. C Prinmer Plus笔记第1-6章

    2024-07-11 02:04:05       0 阅读
  8. MySQL8的备份方案——差异备份(CentOS)

    2024-07-11 02:04:05       0 阅读
  9. C语言——数组指针

    2024-07-11 02:04:05       0 阅读
  10. 【Golang 面试基础题】每日 5 题(一)

    2024-07-11 02:04:05       0 阅读

热门阅读

  1. 无题:天选之子?

    2024-07-11 02:04:05       5 阅读
  2. Flutter——最详细(GestureDetector)使用教程

    2024-07-11 02:04:05       7 阅读
  3. Jenkins构建python项目

    2024-07-11 02:04:05       6 阅读
  4. 使用nodejs进行截图

    2024-07-11 02:04:05       7 阅读
  5. nginx部署vue项目

    2024-07-11 02:04:05       7 阅读
  6. 【网络】SCTP协议概念

    2024-07-11 02:04:05       5 阅读
  7. Git 完整的提交规范教程

    2024-07-11 02:04:05       5 阅读
  8. 【Zoom安全解析】深入Zoom的端到端加密机制

    2024-07-11 02:04:05       6 阅读
  9. Qt QSettings 使用详解:跨平台的配置管理

    2024-07-11 02:04:05       6 阅读
  10. Docker Dockerfile:构建与优化

    2024-07-11 02:04:05       6 阅读
  11. 面试题07-09

    2024-07-11 02:04:05       7 阅读
  12. docker里日志分割的方法

    2024-07-11 02:04:05       6 阅读