Using CLion for LeetCode Practice & Local Debugging

Solving LeetCode problems right in your editor is convenient. Compared with using the website, it’s easier to write your own tests and debug your code before submitting it. This guide covers how to install the plugins, debug your solutions, and submit them from CLion.

0 System Environment

Ubuntu 16.04
CLion 2021.2.2
LeetCode Editor v7.0
C/C+​+​ Single File Execution v1.3 (optional)

1 Install the Plugins

As the saying goes, “A craftsman must sharpen his tools before doing good work.” Before we start solving problems, let’s install two CLion plugins: LeetCode Editor and C/C+​+​ Single File Execution.
Go to File -> Settings -> Plugins.
Search for leetcode to find the LeetCode Editor plugin, then click Install.

Search for single file to find the C/C+​+​ Single File Execution plugin, then click Install.

2 Configure the Plugin

Go to File -> Settings -> Tools -> LeetCode Plugin.
Fill in the fields as described below:

Here’s what each setting does:
URL: leetcode.com or leetcode-cn.com
CodeType: Java, Python, C++, Python3, C, C#, JavaScript, Ruby, Swift, Go, Scala, Kotlin, Rust, PHP
LoginName: Your login username
Password: Your login password
TempFilePath: The temporary file directory where all your LeetCode practice projects will be stored. Create a local directory and enter its path here. I use:/home/liuxiao/Develop/LeetCode
Cutstom Temlate: Enable the custom template option.
CodeFileName: The name of the generated LeetCode file. I recommend using:
$!velocityTool.snakeCaseName($!{question.frontendQuestionId})-${question.titleSlug}
CodeTemplate: This is the key setting: it determines whether you can run the generated file without any extra setup. The template includes the project’s library files, which provide basic C and C++ libraries as well as someone else’s implementations of LeetCode linked lists and trees.
I recommend the following template:

${question.content}

\#include "include/headers.h"

using namespace std;

${question.code}

int main()
{
    Solution s;
    vector data{7, 1, 5, 3, 6, 4};
    auto res = "Hello LeetCode";
    cout< 


3 配置 LeetCode 环境

3.1 下载依赖库文件

使用 git clone 或者直接 copy 以下工程的文件到 /home/liuxiao/Develop/LeetCode : https://github.com/zzh799/LeetCode_LocalDebug_Cpp 例如:
git clone git@github.com:zzh799/LeetCode_LocalDebug_Cpp.git LeetCode

拷贝后文件类似:

3.2 打开工程目录

File -> Open 选择刚才的目录 /home/liuxiao/Develop/LeetCode,然后 Create:

打开后的工程类似下面:

3.3 打开并登录 LeetCode 菜单

在左下角点击方块按钮,然后选择 leetcode 菜单项:

菜单说明如下:
Login 登录:两个网站的登录帐号不互通,切换网站需配置对应的用户
Logout 退出:退出当前账户,如遇到登录错误,尝试先进行退出
Refresh 刷新:在未登录的情况下也可查看刷新加载题目,但是无法提交
PickOne 随机选题目:点击后会随机选择一个题目
Find 查找:输入内容后回车搜索,再次回车搜索下一个,只会搜索题库节点下
Collapse 折叠:折叠全部节点.
Config 配置:快捷跳转到配置界面
Clear 清理:清理配置的缓存目录下的文件,两个网站对应的缓存目录不同,只会清理当前配置的网站下的。部分题目未提交的情况下慎重清理

首先要登录,点击 如果你填写的密码没有错误,右下角则会显示 Login successfuly 的提示。

3.4 创建题目文件

点击右侧 Problem 菜单,打开找到任意题目双击,即可在刚才设置的目录 /home/liuxiao/Develop/LeetCode 中创建临时文件:

3.5 创建工程文件

点击生成的 cpp 源码,在文件空白处,右键选择 Add executable for single c/cpp file,会自动在根目录下的 CMakeLists.txt 中创建可执行项目。

此时就会在你的 CMakeLists.txt 下面加入一行:

add_executable(1-two-sum leetcode/editor/cn/1-two-sum.cpp)

当然你也可以不使用菜单直接手工添加这一行配置。

如果此时在 Run 下面并没有出现 1-two-sum 的可执行选项,可以在工程右键点击 Reload CMake Project。
同时建议使用 File -> Settings -> Build, Execution, Deployment -> CMake 打开自动 Reload CMake 的选项。

4 使用 CLion 本地 Run & Debug

根据 Test Case,修改 main.cpp 文件进行 Debug,例如 Two Sum 问题修改:

int main()
{
    Solution s;
    vector data{7, 1, 5, 3, 6, 4};

    auto res = s.twoSum(data,12);
    cout< 

其余就和调试普通的工程文件没有什么差别了。

5 LeetCode 运行 & 提交

在生成的文件右键点击菜单 leetcode editor menu 可以看到所有 LeetCode 功能: 比如点击 Run Code 结果如下: 比如点击 Submit 结果如下:

参考文献

[1] https://plugins.jetbrains.com/plugin/12132-leetcode-editor [2] https://zhuanlan.zhihu.com/p/79829131 [3] https://github.com/shuzijun/leetcode-editor/blob/master/README_ZH.md [4] https://github.com/zzh799/LeetCode_LocalDebug_Cpp

Add a Comment

Your email address will not be published. Required fields are marked *