如何优雅的使用clion刷leetcode
安装插件:LeetCode Editor)
插件配置:
这样我们每打开一个项目,就会创建类似的文件
我们的项目结构:
我们在题解文件中导入头文件myHeader.h
并将新建的文件添加到cmakelists.txt文件,这样就不会报错了
- myHeader.h
c++
#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
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
c++
#include <iostream>
void runProblem();
int main() {
system("chcp 65001"); // 支持中文
std::cout << "50000个测试用例开始测试!" << std::endl;
runProblem();
return 0;
}
- twoSum.cpp
c++
// 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
方法是最简单的方法,必要情况下,你可以完善如下功能,创建一个生成测试用例的函数,同时输出与正确答案结果不同的测试用例,至于正确结果,可以直接拿题解的就行。例如如下示例
c++
// 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;
}