使用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;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/787968.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

初识CPlusPlus

前言 也是好久没写博客了&#xff0c;那些天也没闲着&#xff0c;去练题去了。实际上练题也可以写练题的博客&#xff0c;但是觉得太简单了些&#xff0c;于是就没有继续写下去。如今又回来写博客&#xff0c;是因为有整理了新的知识C。内容不算多&#xff0c;大多数都是书本上…

接口测试工具Apifox使用以及多环境的配置

下载 Apifox - API 文档、调试、Mock、测试一体化协作平台 - 接口文档工具&#xff0c;接口自动化测试工具&#xff0c;接口Mock工具&#xff0c;API文档工具&#xff0c;API Mock工具&#xff0c;API自动化测试工具 安装 正常安装 , 微信扫码注册 apifox中创建项目 安装idea插…

数学建模美赛入门

数学建模需要的学科知识 高等数学线性代数 有很多算法的掌握是需要高等数学和线代的相关知识 如&#xff1a;灰色预测模型需要微积分知识&#xff1b;神经网络需要用到导数知识&#xff1b;图论和层次分析法等都需要用到矩阵计算的相关知识等&#xff1b; 概率论与数理统计&am…

Xubuntu24.04之设置高性能模式两种方式(二百六十一)

简介: CSDN博客专家,专注Android/Linux系统,分享多mic语音方案、音视频、编解码等技术,与大家一起成长! 优质专栏:Audio工程师进阶系列【原创干货持续更新中……】🚀 优质专栏:多媒体系统工程师系列【原创干货持续更新中……】🚀 优质视频课程:AAOS车载系统+AOSP…

YOLOv8改进 在通道维度上引入注意力机制CPCAttention

一、CPCAttention论文 论文地址:2306.05196 (arxiv.org) 二、CPCAttention结构 Channel prior convolutional attention (CPCA)用于图像分类和目标检测任务中。CPCA能够在卷积神经网络中引入通道相关性,并通过自适应地学习到每个通道的权重,从而提升模型的性能。 CPCA的关…

纯前端如何实现Gif暂停、倍速播放

前言 GIF 我相信大家都不会陌生&#xff0c;由于它被广泛的支持&#xff0c;所以我们一般用它来做一些简单的动画效果。一般就是设计师弄好了之后&#xff0c;把文件发给我们。然后我们就直接这样使用&#xff1a; <img src"xxx.gif"/>这样就能播放一个 GIF …

通过rpmbuild构建Elasticsearch-7.14.2-search-guard的RPM包

系列文章目录 rpmbuild从入门到放弃 search-guard插件使用入门手册 文章目录 系列文章目录前言一、资源准备二、spec文件1.基础信息2.%prep3.%Install4.%file5.%post6.%postun 三、成果演示1.执行构建过程图示例2.执行安装RPM包示例3.进程检查4.访问esApi 总结 前言 不管是源…

Linux--深入理与解linux文件系统与日志文件分析

一、文件与存储系统的 inode 与 block 1.1 硬盘存储 最小存储单位:扇区( sector )每个扇区存储大小:512 字节1.2 文件存取--block block(块),每个 block 块大小为:4k由连续的八个扇区组成一个 block 块是文件索引最小的单位每个 block 块中包括:文件数据文件数据:就…

LeetCode(2)-反转链表、删除链表中等于val的节点、返回链表中的中间节点

一、反转链表 . - 力扣&#xff08;LeetCode&#xff09; 解法1&#xff1a; /*** Definition for singly-linked list.* struct ListNode {* int val;* struct ListNode *next;* };*/ typedef struct ListNode ListNode; struct ListNode* reverseList(struct ListN…

ORA-12537: TNS:连接关闭/Io 异常: Got minus one from a read call

在另外一个数据库建立dblink的时候&#xff0c;发现执行命令报错&#xff1a; 被连接的数据库我也上去过&#xff0c;用工具尝试登陆也报错&#xff1a; IO Error: Got minus one from a read call, connect lapse 1 ms., Authentication lapse 0 ms. Got minus one from a …

Redis过期策略

过期的key集合 Redis会将每个设置了过期时间的key放入到一个独立的字典中&#xff0c;以后会定时遍历这个字典来删除到期的key。除了定时遍历之外&#xff0c;他还会使用惰性策略来删除过期的key&#xff0c;所谓惰性策略就是在客户端访问这个key的时候&#xff0c;redis对key…

生成随机密码

生成8位无重复的密码&#xff08;可以包含数字、大小写字母&#xff09; import random import string character string.digits string.ascii_letters password .join(random.sample(character, 8)) print(f"生成的随机密码为:{password}")

品牌策划秘籍:掌握这些技巧,让你的品牌一炮而红!

作为一名文案策划老司机&#xff0c;这么多年了&#xff0c;总会有一些经验的&#xff0c;这里分享出来&#xff0c;希望能够帮助后来人少走弯路吧。 想要做好品牌和文案策划&#xff0c;首先得做好“侦查”工作。 深入市场&#xff0c;了解行业动态&#xff0c;研究竞争对手…

智能遥测终端机RTU-精确监控 智能运维

智能遥测终端机RTU是物联网领域中一种重要的设备&#xff0c;它的出现无疑为远程监控和数据采集提供了强大的支持。计讯物联智能遥测终端机RTU具备数据采集、处理、通信和控制功能的设备&#xff0c;可以实现对远程设备的监控与控制。它在物联网系统中扮演着桥梁的角色&#xf…

【从零开始实现stm32无刷电机FOC】【理论】【3/6 位置、速度、电流控制】

目录 PID控制滤波单独位置控制单独速度控制单独电流控制位置-速度-电流串级控制 上一节&#xff0c;通过对SVPWM的推导&#xff0c;我们获得了控制电机转子任意受力的能力。本节&#xff0c;我们选用上节得到的转子dq轴解耦的SVPWM形式&#xff0c;对转子受力进行合理控制&…

AE-关键帧

目录 关键帧操作步骤&#xff08;以位置变化为例&#xff09; 1.确定动画起点 2.设置起点的位置属性 3.为起点打上关键帧 4.确定动画终点 5.设置终点的位置属性 改变动画速度 1.选中所有关键帧 2.拖拽 时间反向关键帧 1.选中要反向的关键帧 2.使用时间反向关键帧 …

Websocket 替代方案:如何使用 Firestore 监听实时事件

大家好,我是CodeQi! 一位热衷于技术分享的码仔。 ​在现代 Web 开发中,实时更新功能对于许多应用程序(如聊天应用、协作工具和在线游戏)都是必不可少的。虽然 WebSocket 是一种常用的实时通信技术,但 Google 的 Firestore 也提供了一种强大的替代方案,使得实时监听变得…

数字信号处理教程(3)——z变换

在连续时间域中的每一种分析方法&#xff0c;在离散时间域中想必也能得到对应一种分析方法。连续傅里叶变换对应着离散傅里叶变换&#xff08;DFT&#xff09;&#xff0c;而在拉普拉斯变换则是对应着z变换。z变换能够将信号表示成离散复指数函数的线性组合。连续傅里叶变换可以…

运维系列.Nginx配置文件结构功能总结

运维系列 Nginx配置文件结构功能总结 - 文章信息 - Author: 李俊才 (jcLee95) Visit me at CSDN: https://jclee95.blog.csdn.netMy WebSite&#xff1a;http://thispage.tech/Email: 291148484163.com. Shenzhen ChinaAddress of this article:https://blog.csdn.net/qq_285…

泛微E-Cology getFileViewUrl SSRF漏洞复现

0x01 产品简介 泛微协同管理应用平台e-cology是一套兼具企业信息门户、知识文档管理、工作流程管理、人力资源管理、客户关系管理、项目管理、财务管理、资产管理、供应链管理、数据中心功能的企业大型协同管理平台。 0x02 漏洞概述 泛微E-Cology getFileViewUrl 接口处存在…