当前位置: 首页 > news >正文

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

相关文章:

使用clion刷leetcode

如何优雅的使用clion刷leetcode 安装插件&#xff1a;LeetCode Editor) 插件配置&#xff1a; 这样我们每打开一个项目&#xff0c;就会创建类似的文件 我们的项目结构&#xff1a; 我们在题解文件中导入头文件myHeader.h并将新建的文件添加到cmakelists.txt文件&#xff0c;…...

图解HTTP(5、与 HTTP 协作的 Web 服务器 6、HTTP 首部)

5、与 HTTP 协作的 Web 服务器 一台 Web 服务器可搭建多个独立域名的 Web 网站&#xff0c;也可作为通信路径上的中转服务器提升传输效率。 用单台虚拟主机实现多个域名 在相同的 IP 地址下&#xff0c;由于虚拟主机可以寄存多个不同主机名和域名的 Web 网站&#xff0c;因此…...

JS之防抖和节流

防抖 (debounce) 所谓防抖&#xff0c;就是指触发事件后在 n 秒内函数只能执行一次&#xff0c;如果在 n 秒内又触发了事件&#xff0c;则会重新计算函数执行时间。 ps: 重置普攻&#xff0c;百度翻译要输完停止一定时间后才翻译。 没有防抖和节流的缺点&#xff1a; 函数触发…...

Open3D 点云PCA算法配准(粗配准)

目录 一、概述 1.1PCA配准的原理 1.2PCA配准的应用 二、代码实现 三、实现效果 3.1原始点云 3.2配准后点云 3.3变换矩阵 一、概述 PCA(Principal Component Analysis,主成分分析)是一种用于降维和特征提取的统计方法。在点云处理中,PCA可以用于点云配准(a…...

Transformer中的编码器和解码器结构有什么不同?

Transformer背后的核心概念&#xff1a;注意力机制&#xff1b;编码器-解码器结构&#xff1b;多头注意力等&#xff1b; 例如&#xff1a;The cat sat on the mat&#xff1b; 1、嵌入&#xff1a; 首先&#xff0c;模型将输入序列中的每个单词嵌入到一个高维向量中表示&…...

【深度学习】第5章——卷积神经网络(CNN)

一、卷积神经网络 1.定义 卷积神经网络&#xff08;Convolutional Neural Network, CNN&#xff09;是一种专门用于处理具有网格状拓扑结构数据的深度学习模型&#xff0c;特别适用于图像和视频处理。CNN 通过局部连接和权重共享机制&#xff0c;有效地减少了参数数量&#x…...

fluwx插件实现微信支付

Flutter开发使用fluwx插件实现微信支付&#xff0c;代码量不多&#xff0c;复杂的是安卓和iOS的各种配置。 在 pubspec.yaml 文件中添加fluwx依赖 fluwx: ^4.5.5 使用方法 通过fluwx注册微信Api await Fluwx().registerApi(appId: wxea7a1c53d9e5849d, universalLink: htt…...

k8s核心操作_Deployment的扩缩容能力_Deployment自愈和故障转移能力---分布式云原生部署架构搭建022

然后我们上面说了k8s中的deployment的多副本能力 然后,我们再来看 k8s中的deployment的扩缩容能力 可以看到,对于扩容,要使用 kubectl scale 命令 对于缩容 要使用kubectl scale 命令都是使用这个命令对吧 来试试,可以看到上面命令 首先看看 kubectl get pod 可以看到有…...

P8306 【模板】字典树

题目描述 给定 n 个模式串 s1​,s2​,…,sn​ 和 q 次询问&#xff0c;每次询问给定一个文本串 ti​&#xff0c;请回答 s1​∼sn​ 中有多少个字符串 sj​ 满足 ti​ 是 sj​ 的前缀。 一个字符串 t 是 s 的前缀当且仅当从 s 的末尾删去若干个&#xff08;可以为 0 个&#…...

面试官:讲一下如何终止一个 Promise 继续执行

我们知道 Promise 一旦实例化之后&#xff0c;状态就只能由 Pending 转变为 Rejected 或者 Fulfilled&#xff0c; 本身是不可以取消已经实例化之后的 Promise 了。 但是我们可以通过一些其他的手段来实现终止 Promise 的继续执行来模拟 Promise 取消的效果。 Promise.race …...

linux之常见的coredump原因都有哪些

Core dump通常发生在程序遇到严重错误时&#xff0c;操作系统会生成core文件来记录程序崩溃时的内存、寄存器状态、栈信息等。下面是一些常见的导致core dump的原因&#xff1a; 段错误&#xff08;Segmentation Fault&#xff09;&#xff1a; 当程序尝试访问不允许访问的内存…...

低资源低成本评估大型语言模型(LLMs)

随着新的大型语言模型&#xff08;LLMs&#xff09;的持续发展&#xff0c;从业者发现自己面临着众多选择&#xff0c;需要从数百个可用选项中选择出最适合其特定需求的模型、提示[40]或超参数。例如&#xff0c;Chatbot Arena基准测试平台积极维护着近100个模型&#xff0c;以…...

什么是RPC?有哪些RPC框架?

定义 RPC&#xff08;Remote Procedure Call&#xff0c;远程过程调用&#xff09;是一种允许运行在一台计算机上的程序调用另一台计算机上子程序的技术。这种技术屏蔽了底层的网络通信细节&#xff0c;使得程序间的远程通信如同本地调用一样简单。RPC机制使得开发者能够构建分…...

HTTP有哪些请求方式?

GET&#xff1a;请求指定的资源。例如&#xff0c;用于获取网页内容。POST&#xff1a;向指定资源提交数据&#xff08;例如表单提交&#xff09;。POST请求的数据通常在请求体中。PUT&#xff1a;将请求体中的数据放置到请求URI指定的位置&#xff0c;如果该资源不存在则创建&…...

接口测试课程结构

课程大纲 如图&#xff0c;接下来的阶段课程&#xff0c;依次专项讲解如下专题&#xff0c;能力级别为中级&#xff0c;进阶后基本为中高级&#xff1a; 1.接口基础知识&#xff1b; 2.抓包工具&#xff1b; 3.接口工具&#xff1b; 4.mock服务搭建&#xff08;数据模拟服务&am…...

leetcode--从中序与后序遍历序列构造二叉树

leeocode地址&#xff1a;从中序与后序遍历序列构造二叉树 给定两个整数数组 inorder 和 postorder &#xff0c;其中 inorder 是二叉树的中序遍历&#xff0c; postorder 是同一棵树的后序遍历&#xff0c;请你构造并返回这颗 二叉树 。 示例 1: 输入&#xff1a;inorder …...

西瓜杯CTF(1)

#下班之前写了两个题&#xff0c;后面继续发 Codeinject <?php#Author: h1xaerror_reporting(0); show_source(__FILE__);eval("var_dump((Object)$_POST[1]);"); payload 闭合后面的括号来拼接 POST / HTTP/1.1 Host: 1dc86f1a-cccc-4298-955d-e9179f026d54…...

Kafka 典型问题与排查以及相关优化

Kafka 是一个高吞吐量的分布式消息系统&#xff0c;但在实际应用中&#xff0c;用户经常会遇到一些性能问题和消息堆积的问题。本文将介绍 Kafka 中一些典型问题的原因和排查方法&#xff0c;帮助用户解决问题并优化 Kafka 集群的性能。 一、Topic 消息发送慢&#xff0c;并发性…...

C# 策略模式(Strategy Pattern)

策略模式定义了一系列的算法&#xff0c;并将每一个算法封装起来&#xff0c;使它们可以相互替换。策略模式让算法的变化独立于使用算法的客户。 // 策略接口 public interface IStrategy { void Execute(); } // 具体策略A public class ConcreteStrategyA : IStra…...

【初阶数据结构】1.算法复杂度

文章目录 1.数据结构前言1.1 数据结构1.2 算法1.3 如何学好数据结构和算法 2.算法效率2.1 复杂度的概念2.2 复杂度的重要性 3.时间复杂度3.1 大O的渐进表示法3.2 时间复杂度计算示例3.2.1 示例13.2.2 示例23.2.3 示例33.2.4 示例43.2.5 示例53.2.6 示例63.2.7 示例7 4.空间复杂…...

(图文详解)小程序AppID申请以及在Hbuilderx中运行

今天小编给大家带来了如何去申请APPID&#xff0c;如果你是小程序的开发者&#xff0c;就必须要这个id。 申请步骤 到小程序注册页面&#xff0c;注册一个小程序账号 微信公众平台 填完信息后提交注册 会在邮箱收到 链接激活账号 确认。邮箱打开链接后&#xff0c;会输入实…...

科技创新引领水利行业升级:深入分析智慧水利解决方案的核心价值,展望其在未来水资源管理中的重要地位与作用

目录 引言 一、智慧水利的概念与内涵 二、智慧水利解决方案的核心价值 1. 精准监测与预警 2. 优化资源配置 3. 智能运维管理 4. 公众参与与决策支持 三、智慧水利在未来水资源管理中的重要地位与作用 1. 推动水利行业转型升级 2. 保障国家水安全 3. 促进生态文明建设…...

ExcelVBA运用Excel的【条件格式】(三)

ExcelVBA运用Excel的【条件格式】&#xff08;三&#xff09;前面知识点回顾1. 访问 FormatConditions 集合 Range.FormatConditions2. 添加条件格式 FormatConditions.Add 方法语法表达式。添加 (类型、 运算符、 Expression1、 Expression2)其中 TextOperator:***&am…...

coco数据集格式计算mAP的python脚本

目录 背景说明COCOeval 计算mAPtxt文件转换为coco json 格式自定义数据集标注 背景说明 在完成YOLOv5模型移植&#xff0c;运行在板端后&#xff0c;通常需要衡量板端运行的mAP。 一般需要两个步骤 步骤一&#xff1a;在板端批量运行得到目标检测结果&#xff0c;可保存为yol…...

Linux学习——Linux中无法使用ifconfg命令

Linux学习——Linux中无法使用ifconfg命令&#xff1f; &#x1f49d;&#x1f49d;&#x1f49d;欢迎来到我的博客&#xff0c;很高兴能够在这里和您见面&#xff01;希望您在这里可以感受到一份轻松愉快的氛围&#xff0c;不仅可以获得有趣的内容和知识&#xff0c;也可以畅…...

二分查找3

1. 有序数组中的单一元素&#xff08;540&#xff09; 题目描述&#xff1a; 算法原理&#xff1a; 二分查找解题关键就在于去找到数组的二段性&#xff0c;这里数组的二段性是从单个数字a开始出现然后分隔出来的&#xff0c;如果mid落入左半部分那么当mid为偶数时nums[mid1]…...

从零开始学习嵌入式----C语言框架梳理与后期规划

目录 一、环境搭建. 二、见解 三、C语言框架梳理 四、嵌入式学习规划流程图&#xff08;学习顺序可能有变&#xff09; 一、环境搭建. C语言是一门编程语言&#xff0c;在学习的时候要准备好环境。我个人比较喜欢用VS,具体怎么安装请百度。学习C语言的时候&#xff0c;切忌…...

ESP32 步进电机精准控制:打造高精度 DIY 写字机器人,实现流畅书写体验

摘要: 想让你的 ESP32 不再仅仅是控制灯光的工具吗&#xff1f; 本文将带你使用 ESP32 开发板、步进电机和简单的机械结构打造一个能够自动写字的机器人。我们将深入浅出地讲解硬件连接、软件代码以及控制逻辑&#xff0c;并提供完整的项目代码和电路图&#xff0c;即使是 Ardu…...

传知代码-图神经网络长对话理解(论文复现)

代码以及视频讲解 本文所涉及所有资源均在传知代码平台可获取 概述 情感识别是人类对话理解的关键任务。随着多模态数据的概念&#xff0c;如语言、声音和面部表情&#xff0c;任务变得更加具有挑战性。作为典型解决方案&#xff0c;利用全局和局部上下文信息来预测对话中每…...

部署前端项目

常见部署方式有&#xff1a;静态托管服务、服务器部署 1. 静态托管服务 使用平台部署代码&#xff0c;比如 GitHub。 | 创建一个仓库&#xff0c;仓库名一般是 yourGithubName.github.io。 | 将打包后的静态文件文件上传到仓库。 | 在“Settings”&#xff08;选项&#xff0…...