当前位置: 首页 > 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.空间复杂…...

TDengine 快速体验(Docker 镜像方式)

简介 TDengine 可以通过安装包、Docker 镜像 及云服务快速体验 TDengine 的功能&#xff0c;本节首先介绍如何通过 Docker 快速体验 TDengine&#xff0c;然后介绍如何在 Docker 环境下体验 TDengine 的写入和查询功能。如果你不熟悉 Docker&#xff0c;请使用 安装包的方式快…...

Prompt Tuning、P-Tuning、Prefix Tuning的区别

一、Prompt Tuning、P-Tuning、Prefix Tuning的区别 1. Prompt Tuning(提示调优) 核心思想:固定预训练模型参数,仅学习额外的连续提示向量(通常是嵌入层的一部分)。实现方式:在输入文本前添加可训练的连续向量(软提示),模型只更新这些提示参数。优势:参数量少(仅提…...

Debian系统简介

目录 Debian系统介绍 Debian版本介绍 Debian软件源介绍 软件包管理工具dpkg dpkg核心指令详解 安装软件包 卸载软件包 查询软件包状态 验证软件包完整性 手动处理依赖关系 dpkg vs apt Debian系统介绍 Debian 和 Ubuntu 都是基于 Debian内核 的 Linux 发行版&#xff…...

Auto-Coder使用GPT-4o完成:在用TabPFN这个模型构建一个预测未来3天涨跌的分类任务

通过akshare库&#xff0c;获取股票数据&#xff0c;并生成TabPFN这个模型 可以识别、处理的格式&#xff0c;写一个完整的预处理示例&#xff0c;并构建一个预测未来 3 天股价涨跌的分类任务 用TabPFN这个模型构建一个预测未来 3 天股价涨跌的分类任务&#xff0c;进行预测并输…...

现代密码学 | 椭圆曲线密码学—附py代码

Elliptic Curve Cryptography 椭圆曲线密码学&#xff08;ECC&#xff09;是一种基于有限域上椭圆曲线数学特性的公钥加密技术。其核心原理涉及椭圆曲线的代数性质、离散对数问题以及有限域上的运算。 椭圆曲线密码学是多种数字签名算法的基础&#xff0c;例如椭圆曲线数字签…...

C++ 求圆面积的程序(Program to find area of a circle)

给定半径r&#xff0c;求圆的面积。圆的面积应精确到小数点后5位。 例子&#xff1a; 输入&#xff1a;r 5 输出&#xff1a;78.53982 解释&#xff1a;由于面积 PI * r * r 3.14159265358979323846 * 5 * 5 78.53982&#xff0c;因为我们只保留小数点后 5 位数字。 输…...

NLP学习路线图(二十三):长短期记忆网络(LSTM)

在自然语言处理(NLP)领域,我们时刻面临着处理序列数据的核心挑战。无论是理解句子的结构、分析文本的情感,还是实现语言的翻译,都需要模型能够捕捉词语之间依时序产生的复杂依赖关系。传统的神经网络结构在处理这种序列依赖时显得力不从心,而循环神经网络(RNN) 曾被视为…...

如何理解 IP 数据报中的 TTL?

目录 前言理解 前言 面试灵魂一问&#xff1a;说说对 IP 数据报中 TTL 的理解&#xff1f;我们都知道&#xff0c;IP 数据报由首部和数据两部分组成&#xff0c;首部又分为两部分&#xff1a;固定部分和可变部分&#xff0c;共占 20 字节&#xff0c;而即将讨论的 TTL 就位于首…...

Element Plus 表单(el-form)中关于正整数输入的校验规则

目录 1 单个正整数输入1.1 模板1.2 校验规则 2 两个正整数输入&#xff08;联动&#xff09;2.1 模板2.2 校验规则2.3 CSS 1 单个正整数输入 1.1 模板 <el-formref"formRef":model"formData":rules"formRules"label-width"150px"…...

sipsak:SIP瑞士军刀!全参数详细教程!Kali Linux教程!

简介 sipsak 是一个面向会话初始协议 (SIP) 应用程序开发人员和管理员的小型命令行工具。它可以用于对 SIP 应用程序和设备进行一些简单的测试。 sipsak 是一款 SIP 压力和诊断实用程序。它通过 sip-uri 向服务器发送 SIP 请求&#xff0c;并检查收到的响应。它以以下模式之一…...