如何优雅的使用clion刷leetcode
安装插件:LeetCode Editor)
插件配置:
这样我们每打开一个项目,就会创建类似的文件
我们的项目结构:
我们在题解文件中导入头文件myHeader.h
并将新建的文件添加到cmakelists.txt文件,这样就不会报错了
- myHeader.h
#ifndef MY_HEADER_H
#define MY_HEADER_H#include <iostream>
#include <vector>
#include <string>
#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;#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})# 添加自定义编译选项,选择运行的题解
add_definitions(-DPROBLEM_FILE="\\\"twoSum.cpp\\\"")target_include_directories(LeetCodeTime PUBLIC ${PROJECT_SOURCE_DIR}/include)
这里的
add_definitions(-DPROBLEM_FILE="\\\"twoSum.cpp\\\"")
是为了方便调试,以后要调试哪个题解,就将哪个题解的文件名替换掉twoSum.cpp
,还要记得修改file(GLOB SRC_FILES ${PROJECT_SOURCE_DIR}/leetcode/editor/en/twoSum.cpp)
,这样就能调试了~
- main.cpp
#include <iostream>
using namespace std;
void runProblem();
int main() {cout << "Running problem: " << PROBLEM_FILE << 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)
class CorrectSolution {
public://这是官方题解vector<int> twoSum(vector<int>& nums, int target) {unordered_map<int, int> map;for (int i = 0; i < nums.size(); ++i) {int complement = target - nums[i];if (map.find(complement) != map.end()) {return {map[complement], i};}map[nums[i]] = i;}return {};}
};
// 生成测试用例
void generateTestCases(vector<pair<vector<int>, int>>& testCases) {testCases.push_back({{2, 7, 11, 15}, 9});testCases.push_back({{3, 2, 4}, 6});testCases.push_back({{3, 3}, 6});// 可以添加更多测试用例
}
//调试逻辑
void runProblem() {Solution solution;CorrectSolution correctSolution;vector<pair<vector<int>, int>> testCases;generateTestCases(testCases);for (auto& testCase : testCases) {vector<int> nums = testCase.first;int target = testCase.second;vector<int> result = solution.twoSum(nums, target);vector<int> correctResult = correctSolution.twoSum(nums, target);// 比较结果,如果不同,则输出错误的测试用例if (result != correctResult) {cout << "Input: nums = [";for (size_t i = 0; i < nums.size(); ++i) {cout << nums[i] << (i < nums.size() - 1 ? ", " : "");}cout << "], target = " << target << endl;cout << "Your Output: [";for (size_t i = 0; i < result.size(); ++i) {cout << result[i] << (i < result.size() - 1 ? ", " : "");}cout << "]" << endl;cout << "Correct Output: [";for (size_t i = 0; i < correctResult.size(); ++i) {cout << correctResult[i] << (i < correctResult.size() - 1 ? ", " : "");}cout << "]" << endl;cout << "Test case failed." << endl;} else {cout << "Test case passed." << endl;}cout << "--------------------" << endl;}
}
注意,再clion上提交代码时,只会提交以下被包裹的代码,其他代码是不会被提交的,
虽然通过这个模板,我们到时候只需要修改生成测试用例的代码,官方题解和修改cmake文件就能进行调试了~
,总体来说并不复杂,开始时可能不太熟练,但写多一两次就很快了,同时,生成测试用例的代码并不是没有用,打比赛时的测试用例也是要自己生成的,写熟练以后对以后打比赛还是有点用的。//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)