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

设计模式和设计原则回顾

设计模式和设计原则回顾 23种设计模式是设计原则的完美体现,设计原则设计原则是设计模式的理论基石, 设计模式 在经典的设计模式分类中(如《设计模式:可复用面向对象软件的基础》一书中),总共有23种设计模式,分为三大类: 一、创建型模式(5种) 1. 单例模式(Sing…...

【JavaEE】-- HTTP

1. HTTP是什么&#xff1f; HTTP&#xff08;全称为"超文本传输协议"&#xff09;是一种应用非常广泛的应用层协议&#xff0c;HTTP是基于TCP协议的一种应用层协议。 应用层协议&#xff1a;是计算机网络协议栈中最高层的协议&#xff0c;它定义了运行在不同主机上…...

Zustand 状态管理库:极简而强大的解决方案

Zustand 是一个轻量级、快速和可扩展的状态管理库&#xff0c;特别适合 React 应用。它以简洁的 API 和高效的性能解决了 Redux 等状态管理方案中的繁琐问题。 核心优势对比 基本使用指南 1. 创建 Store // store.js import create from zustandconst useStore create((set)…...

CMake基础:构建流程详解

目录 1.CMake构建过程的基本流程 2.CMake构建的具体步骤 2.1.创建构建目录 2.2.使用 CMake 生成构建文件 2.3.编译和构建 2.4.清理构建文件 2.5.重新配置和构建 3.跨平台构建示例 4.工具链与交叉编译 5.CMake构建后的项目结构解析 5.1.CMake构建后的目录结构 5.2.构…...

Golang dig框架与GraphQL的完美结合

将 Go 的 Dig 依赖注入框架与 GraphQL 结合使用&#xff0c;可以显著提升应用程序的可维护性、可测试性以及灵活性。 Dig 是一个强大的依赖注入容器&#xff0c;能够帮助开发者更好地管理复杂的依赖关系&#xff0c;而 GraphQL 则是一种用于 API 的查询语言&#xff0c;能够提…...

【项目实战】通过多模态+LangGraph实现PPT生成助手

PPT自动生成系统 基于LangGraph的PPT自动生成系统&#xff0c;可以将Markdown文档自动转换为PPT演示文稿。 功能特点 Markdown解析&#xff1a;自动解析Markdown文档结构PPT模板分析&#xff1a;分析PPT模板的布局和风格智能布局决策&#xff1a;匹配内容与合适的PPT布局自动…...

MySQL 8.0 OCP 英文题库解析(十三)

Oracle 为庆祝 MySQL 30 周年&#xff0c;截止到 2025.07.31 之前。所有人均可以免费考取原价245美元的MySQL OCP 认证。 从今天开始&#xff0c;将英文题库免费公布出来&#xff0c;并进行解析&#xff0c;帮助大家在一个月之内轻松通过OCP认证。 本期公布试题111~120 试题1…...

自然语言处理——Transformer

自然语言处理——Transformer 自注意力机制多头注意力机制Transformer 虽然循环神经网络可以对具有序列特性的数据非常有效&#xff0c;它能挖掘数据中的时序信息以及语义信息&#xff0c;但是它有一个很大的缺陷——很难并行化。 我们可以考虑用CNN来替代RNN&#xff0c;但是…...

什么?连接服务器也能可视化显示界面?:基于X11 Forwarding + CentOS + MobaXterm实战指南

文章目录 什么是X11?环境准备实战步骤1️⃣ 服务器端配置(CentOS)2️⃣ 客户端配置(MobaXterm)3️⃣ 验证X11 Forwarding4️⃣ 运行自定义GUI程序(Python示例)5️⃣ 成功效果![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/55aefaea8a9f477e86d065227851fe3d.pn…...

A2A JS SDK 完整教程:快速入门指南

目录 什么是 A2A JS SDK?A2A JS 安装与设置A2A JS 核心概念创建你的第一个 A2A JS 代理A2A JS 服务端开发A2A JS 客户端使用A2A JS 高级特性A2A JS 最佳实践A2A JS 故障排除 什么是 A2A JS SDK? A2A JS SDK 是一个专为 JavaScript/TypeScript 开发者设计的强大库&#xff…...