比较喜欢用VSCode做编辑器,所以这就是如何配置VSCode用来刷Leetcode题目

overview.png

环境配置:

  • 首先下载VSCode,安装插件LeetcodeC++
    • image.png
    • image.png
      • 使用这个插件记得切换成中国站cn,否则登录有问题
  • 然后配置一下C++编译环境
    • 使用WinBuid工具直接下载所有安装包
    • 配置Path
  • Leetcode的插件需要Node.js支持去官网下载一个,默认安装即可、
  • C++的插件也需要配置一下
  • image.png

然后给几个

vscode配置文件

image.png
这是启动项配置 launch.json

  1. {
  2. // Use IntelliSense to learn about possible attributes.
  3. // Hover to view descriptions of existing attributes.
  4. // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  5. "version": "0.2.0",
  6. "configurations": [
  7. {
  8. "name": "g++.exe - 生成和调试活动文件",
  9. "type": "cppdbg",
  10. "request": "launch",
  11. "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
  12. "args": [],
  13. "stopAtEntry": false,
  14. "cwd": "${workspaceFolder}",
  15. "environment": [],
  16. "externalConsole": true, // 调试时是否显示控制台窗口,一般设置为true显示控制台
  17. "MIMode": "gdb",
  18. "miDebuggerPath": "C:\\sooooft\\WinBuild\\files\\bin\\gdb.exe",
  19. "setupCommands": [
  20. {
  21. "description": "为 gdb 启用整齐打印",
  22. "text": "-enable-pretty-printing",
  23. "ignoreFailures": true
  24. }
  25. ],
  26. "preLaunchTask": "g++"
  27. }
  28. ]
  29. }

修改编译任务,tasks.json

  1. {
  2. // See https://go.microsoft.com/fwlink/?LinkId=733558
  3. // for the documentation about the tasks.json format
  4. "version": "2.0.0",
  5. "tasks": [
  6. {
  7. "type": "shell",
  8. "label": "g++", //必须与 launch.json 文件中 "preLaunchTask"的值一致。值的设置看个人喜好。
  9. "command": "C:\\sooooft\\WinBuild\\files\\bin\\g++.exe",
  10. "args": [
  11. "-g",
  12. "${file}",
  13. "-o",
  14. "${fileDirname}\\${fileBasenameNoExtension}.exe"
  15. ],
  16. "options": {
  17. "cwd": "C:\\sooooft\\WinBuild\\files\\bin"
  18. },
  19. "problemMatcher": [
  20. "$gcc"
  21. ]
  22. }
  23. ]
  24. }

配置项

  1. {
  2. "files.defaultLanguage": "cpp",
  3. "editor.formatOnType": true,
  4. "editor.suggest.snippetsPreventQuickSuggestions": false,
  5. "editor.acceptSuggestionOnEnter": "off",
  6. "code-runner.runInTerminal": true,
  7. "code-runner.executorMap": {
  8. "c": "cd $dir && gcc '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c11 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'",
  9. "cpp": "cd $dir && g++ '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc -std=c++11 -fexec-charset=GBK && &'$dir$fileNameWithoutExt'"
  10. },
  11. "code-runner.saveFileBeforeRun": true,
  12. "code-runner.preserveFocus": true,
  13. "code-runner.clearPreviousOutput": false,
  14. "code-runner.ignoreSelection": true,
  15. "C_Cpp.clang_format_sortIncludes": true
  16. }

完了再写一个

测试程序试试

看能不能成功

  1. #include <stdio.h>
  2. #include <Windows.h>
  3. int main(){
  4. printf("Hello World!"); //c的语法好像vscode不喜欢
  5. system("pause"); //避免调试模式下自动关闭窗口
  6. return 0;
  7. }

image.png
ok

VSCode写Leetcode并且可调试

分析一下这个插件的用法,可以发现真正提交的部分是红色框选的
我们如果在其

  • 上面写一些头文件include
  • 下方写一下测试入口mian

就可以直接在本地进行调试,这样岂不是方便得很。
image.png
配合VSCode的“用户代码片段”/snippet 就可以很方便把这个做成模板,快速调用。
这里因为文件名是中文还有符号,所以g++没法识别
可以直接复制到一个新的cpp中,一样可以点击测试啥的,因为题目的信息写在注释里面了。
image.png
这里我随便写了个测试

snippet可以用这个,在使用过程中可以不断更新。

  1. {
  2. "include for leetcode": {
  3. "prefix": "l-head",
  4. "body": [
  5. "#include <stdio.h>",
  6. "#include <iostream>",
  7. "#include <vector>",
  8. "#include <algorithm>",
  9. "#include <math.h>",
  10. "#include <Windows.h>",
  11. "using namespace std;",
  12. "",
  13. "void func1(vector<int> &nums, int k);",
  14. ],
  15. "description": "head of leetcode"
  16. },
  17. "entrance for leetcode": {
  18. "prefix": "l-entrance",
  19. "body": [
  20. "int main(){",
  21. "\tSolution so;",
  22. "\tvector<int> vec(3,1);",
  23. "\tint sz = 0;",
  24. "\tfunc1(vec, sz);",
  25. "\tso.topKFrequent(vec, 22);",
  26. "\tfunc1(vec, sz);",
  27. "",
  28. "\tsystem(\"pause\");",
  29. "\treturn 0;",
  30. "}",
  31. "void func1(vector<int> &nums, int k){",
  32. "\tfor (size_t i = 0; i < nums.size(); ++i)",
  33. "\t{",
  34. "\t\tcout << nums[i] << \" \";",
  35. "\t}",
  36. "\tcout << endl;",
  37. "}",
  38. ],
  39. "description": "entrance of leetcode"
  40. },
  41. }