# wwm.leetcode.helper **Repository Path**: wwmin/wwm.leetcode.helper ## Basic Information - **Project Name**: wwm.leetcode.helper - **Description**: leetcode c#答题代码自动生成器 - **Primary Language**: C# - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: https://www.jianshu.com/u/6a87586ed87c - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 1 - **Created**: 2021-11-01 - **Last Updated**: 2024-12-11 ## Categories & Tags **Categories**: Uncategorized **Tags**: LeetCode, Csharp ## README # [wwm.LeetCodeHelper](https://gitee.com/wwmin/wwm.leetcode.helper.git) [![wwm.LeetCodeHelper](https://img.shields.io/nuget/v/wwm.leetcodehelper.svg)](https://www.nuget.org/packages/OSharp.Core/) [![wwm.LeetCodeHelper](https://img.shields.io/nuget/dt/wwm.leetcodehelper.svg)](https://www.nuget.org/packages/OSharp.Core/) [![GitHub license](https://img.shields.io/badge/license-Apache%202-blue.svg)](https://raw.githubusercontent.com/i66soft/osharp-ns20/master/LICENSE) 仓库地址:[https://gitee.com/wwmin/wwm.leetcode.helper](https://gitee.com/wwmin/wwm.leetcode.helper) # 1. 说明 wwm.LeetCodeHelper是一款帮助在本地用C#做LeetCode题的一个库,具有自动拉取题生成csharp文件,自动生成测试用例,自动完成测试对比等等的功能。 - 适用语言范围:C# - 使用LeetCode版本:国内版 # 2. 安装 - [.NET CLI](https://www.nuget.org/packages/wwm.LeetCodeHelper) ``` dotnet add package wwm.LeetCodeHelper ``` - [Package Manager](https://www.nuget.org/packages/wwm.LeetCodeHelper) ``` Install-Package wwm.LeetCodeHelper ``` # 3. 使用方式 创建控制台应用程序 ## 3.1 创建项目步骤 新建一个文件夹,然后cmd到此文件夹路径执行如下命令 ``` //新建项目 dotnet new console //添加leetcode帮助依赖 dotnet add package wwm.leetcodehelper //添加操作配置文件依赖 dotnet add package Microsoft.Extensions.Configuration dotnet add package Microsoft.Extensions.Configuration.UserSecrets //初始化用户机密文件 dotnet user-secrets init //将web端的LEETCODE_SESSION cookie设置到机密文件,如下 dotnet user-secrets set "cookie_login:LEETCODE_SESSION" "your cookie" //查看已添加的机密信息 dotnet user-secrets list //查看package list dotnet list package //更新pacakge ,指定version dotnet add package wwm.leetcodehelper -v 0.10.0 //安装依赖包 dotnet restore ``` ## 3.2 添加.gitignore 文件 ``` .vs/ bin/ obj/ /content cookies.txt ``` 只需要创建一次此控制台应用程序,后面的题库内容都是一个一个类文件 在program.cs文件中添加如下内容 ```c# using Microsoft.Extensions.Configuration; #region 初始化 using Microsoft.Extensions.Configuration; #region 初始化 #if false //方法一: 直接明文保存LEETCODE_SESSION var LEETCODE_SESSION = ""; #endif // 使用本地机密文件secrets.json 然后从机密中读取登录信息, 此方法更好的保护自己的账号 //方法二: 使用vs IDE //步骤1: 添加依赖 Microsoft.Extensions.Configuration //步骤2: vs中右键项目然后选择管理用户机密,然后在打开的secrets.json中添加 /* { "cookie_login": { "LEETCODE_SESSION": "your leetcode_session cookie", } } */ //方法三: 使用dotnet cli //步骤1: 添加依赖 Microsoft.Extensions.Configuration //步骤2: 启用机密,使用.net cli命令 `dotnet user-secrets init` //步骤3: 设置机密, `dotnet user-secrets set "cookie_login:LEETCODE_SESSION" "your cookie"` //步骤4: 查看 `dotnet user-secrets list` // 更多dotnet cli 可参考:https://docs.microsoft.com/zh-cn/aspnet/core/security/app-secrets?view=aspnetcore-6.0&tabs=windows var config = new ConfigurationBuilder().SetBasePath(AppDomain.CurrentDomain.BaseDirectory).AddUserSecrets().Build(); var LEETCODE_SESSION = config.GetSection("cookie_login:LEETCODE_SESSION")?.Value; if (!string.IsNullOrEmpty(LEETCODE_SESSION)) await LeetCodeHelper.InitAsync(LEETCODE_SESSION); else throw new Exception("请传入LEETCODE_SESSION"); #endregion //1. 传入url或titleSlug获取题目 //2. 传入题号获取题目 //3. 参数为空时获取当天的每日一题 //如果已获取题目则会自动开始测试 await LeetCodeHelper.Run(); Console.Write("按任意键退出..."); Console.ReadKey(); ``` # 4. 初始化用户信息 为了安全起见,建议用户将自己的`cookie` `LEETCODE_SESSION`值存储到`secrets.json`文件中,这样将自己的代码库上传到git远程仓库就不会担心自己的leet-code信息被盗取了 具体说明:参考上段代码中的注释部分 > 如何获取LEETCODE_SESSION? > 获取leetcode网站cookie方法: > 登录leetcode网站后,打开控制台,找到控制台的Application面板在左侧的Storage项中找到Cookies然后选中`https://leetcode.cn` 右侧有出现该网站的cookie目录,寻找到`LEETCODE_SESSION`选中后,复制后面的Value值即可。 # 5. 刷题方式:1.自定义url 2.每日一题 ## 5.1 自定义url 将自己想要刷的题目的url粘贴到`program.cs`文件中的`url`变量中,然后运行程序即可,程序会自动获取题目. 如果此题已经存在在本地, 则会开启测试模式. ## 5.2 每日一题模式 自动获取当天的每日一题,如果此题已经存在本地, 则会开启测试模式 ## 5.3 自定义题号 将自己想要刷的题目的题号粘贴到`program.cs`文件中的`questionId`变量中,然后运行程序即可,程序会自动获取题目. 如果此题已经存在在本地, 则会开启测试模式. # 6. 获取题目 在选择自定义url模式刷题时,获取题目需要添加url内容 ``` string url = "https://leetcode.cn/problems/two-sum/" ``` 然后F5运行, 控制台中内容如下: ``` 已从接口获取数据 将数据写入文件:two-sum.txt 文件已写入:D:\work\learn\dotnet\wwm.LeetCodeHelper\wwm.LeetCodeHelper.Test\Content\two-sum.txt 文件名已拷贝到剪贴板: two-sum.txt 文件已写入:D:\work\learn\dotnet\wwm.LeetCodeHelper\wwm.LeetCodeHelper.Test\Solutions\two-sum.cs 文件名已拷贝到剪贴板: two-sum.cs 按任意键退出... ``` 文件名已经复制到了剪贴板中,在Visual Studio中按`Ctrl+Shift+T`快捷键弹出框中,粘贴文件名后查找到回车即可,此处为了节省文件变多后查找刚下载的文件的时间 内容如下: ```C# namespace Solutions; /// /// 1556. 通过翻转子数组使两个数组相等 /// difficulty: Easy /// public class CanBeEqualSolution : ITest { private class Data : DataAttribute { public override IEnumerable GetData() { yield return new object[] { StringTo("[1,2,3,4]").ToArray(), StringTo("[2,4,1,3]").ToArray(), true }; yield return new object[] { StringTo("[7]").ToArray(), StringTo("[7]").ToArray(), true }; yield return new object[] { StringTo("[3,7,9]").ToArray(), StringTo("[3,7,11]").ToArray(), false }; } } [Data] public bool CanBeEqual(int[] target, int[] arr) { return default; } } ``` 会在题内容上面加上测试用例, 如果是自定义刷题模式时,将url置空后就会进入执行实现ITest接口的类, 如果是每日刷题,会自动判断题目是否已经下载,如果已下载则进入执行实现ITest的接口类。 # 7. 配置vscode debug环境 如果使用visual studio跳过此步骤, 如果使用vscode则需要安装vscode插件 [C#](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp) # 8. 配置launch.json 和 tasks.json 如果使用visual studio跳过此步骤, 如果使用vscode则需要配置launch.json 和 tasks.json 按F5 在下拉的选择环境框中选择 `.NET5+ and .NET Core` 此时会创建launch.json,注意此时要修改configurations:program 下的参数: 将`"${workspaceFolder}/bin/Debug//.dll"`, 中的``修改为你自己的`net6.0`或这你自己的版本(此处推荐最新的net6.0), 然后修改``为你自己的项目名称,如我自己的`leetcode_test`, 然后将configurations:console换成`integratedTerminal`. 此时再在Program.cs文件下按F5,应该会弹出 `Count not find the task 'build'`警告框,此时选择`Configure Task`按钮 然后在弹出选择框中选择`Create task.json file from template`,然后再在弹出的选择框中选择`.NET Core`.此时就在.vscode文件夹中创建好了tasks.json # 9. 运行测试 编写你的算法代码 ```c# using wwm.LeetCodeHelper.Servers; namespace Solutions; /// /// 1. 两数之和 /// https://leetcode.cn/problems/two-sum/ /// difficulty: Easy /// public class TwoSumSolution : ITest { private class Data : DataAttribute { public Data() { IgnoreOrder = true; } public override IEnumerable GetData() { yield return new object[] { StringTo("[2,7,11,15]").ToArray(), 9, StringTo("[0,1]").ToArray() }; yield return new object[] { StringTo("[3,2,4]").ToArray(), 6, StringTo("[1,2]").ToArray() }; yield return new object[] { StringTo("[3,3]").ToArray(), 6, StringTo("[0,1]").ToArray() }; } } [Data] public int[] TwoSum(int[] nums, int target) { int[] o = new int[2]; int i, j, k = 0; for (i = 0; i < nums.Length; i++) { for (j = i + 1; j < nums.Length; j++) { if ((nums[i] + nums[j]) == target) { o[0] = i; o[1] = j; k = 1; } } if (k == 1) break; } return o; } } ``` F5执行,结果如下: ``` ------->TwoSumSolution TwoSum: 执行结果正确: [0,1] TwoSum: 执行结果正确: [1,2] TwoSum: 执行结果正确: [0,1] _______测试结果: 3/3 ,耗时:97ms________ 按任意键退出... ``` 如果执行中有错误答案,会给出预期值和运行结果值,如下 ``` ------->TwoSumSolution TwoSum: 执行结果正确: [0,1] TwoSum: 执行结果错误. 预期值:[1,2] 结果值:[0,0] TwoSum: 执行结果正确: [0,1] _______测试结果: 2/3 ,耗时:82ms________ 按任意键退出... ``` 控制台中也会有颜色提示,正确的为绿色,错误的为红色。 当测试完成后,需要将类的继承ITest去掉,这样下次就不会测试该类了。 该库暂时仍在完善中,如遇到问题可提issue。 # 10. 计划/TODO 1. 实现一个可视化树结构的功能 # 11. 编译错误解答 问题1: 如果出现了如下错误 ``` Exception has occurred: CLR/System.InvalidOperationException “System.InvalidOperationException”类型的异常在 System.Console.dll 中发生,但未在用户代码中进行处理: 'Cannot read keys when either application does not have a console or when console input has been redirected. Try Console.Read.' ``` 可将launch.json文件下的configurations:console换成`integratedTerminal`,或者删除Program.cs文件中的`Console.ReadKey();`这行代码 问题2: 如果出现了如下错误 ``` “Microsoft.Playwright.PlaywrightException”类型的异常在 System.Private.CoreLib.dll 中发生,但未在用户代码中进行处理: 'Chromium distribution 'chrome' is not found at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome Run "playwright install chrome"' ``` 需要安装playwright, 前往playwright官网 执行 ``` # Install required browsers pwsh bin\Debug\netX\playwright.ps1 install ``` mac 下执行pwsh时会出现`bash: pwsh: command not found`则需要下载安装powershell,地址[https://github.com/PowerShell/PowerShell/releases](https://github.com/PowerShell/PowerShell/releases) 选择合适的系统下载安装,然后再次执行 ``` # Install required browsers pwsh bin\Debug\netX\playwright.ps1 install # max下 \ 应变成 / ``` 如果出现 ``` Unhandled exception. Microsoft.Playwright.PlaywrightException: Chromium distribution 'chrome' is not found at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome Run "playwright install chrome" at Microsoft.Playwright.Transport.Connection.SendMessageToServerAsync[T](String guid, String method, Object args) ``` 则需要执行 ``` playwright install chrome ``` # 12. 相关连接 - [作者刷leetcode代码库 gitee](https://gitee.com/wwmin/LeetCodeCSharp) - [作者对wwm.leetcodehelper库的问题收集库 gitee](https://gitee.com/wwmin/wwm.leetcode.helper.issue) - [作者的Leetcode](https://leetcode-cn.com/u/wwmin/) # 13. 许可证 / License [wwm.LeetCodeHelper](https://gitee.com/wwmin/wwm.leetcode.helper.git) 采用 [Apache License 2.0](https://gitee.com/wwmin/wwm.leetcode.helper/raw/master/LICENSE) 开源许可证。 [wwm.LeetCodeHelper](https://gitee.com/wwmin/wwm.leetcode.helper.git) uses the [Apache License 2.0](https://gitee.com/wwmin/wwm.leetcode.helper/raw/master/LICENSE) open source license. >作者:wwmin >微信公众号: DotNet技术说 >本文链接:[https://www.jianshu.com/p/b038e21032ab](https://www.jianshu.com/p/b038e21032ab) >评论和私信会在第一时间回复。转载请注明出处! >如果您觉得文章对您有帮助,关注点赞,您的鼓励是博主的最大动力!