wwm.LeetCodeHelper
0.8.7
See the version list below for details.
dotnet add package wwm.LeetCodeHelper --version 0.8.7
NuGet\Install-Package wwm.LeetCodeHelper -Version 0.8.7
<PackageReference Include="wwm.LeetCodeHelper" Version="0.8.7" />
paket add wwm.LeetCodeHelper --version 0.8.7
#r "nuget: wwm.LeetCodeHelper, 0.8.7"
// Install wwm.LeetCodeHelper as a Cake Addin #addin nuget:?package=wwm.LeetCodeHelper&version=0.8.7 // Install wwm.LeetCodeHelper as a Cake Tool #tool nuget:?package=wwm.LeetCodeHelper&version=0.8.7
wwm.LeetCodeHelper
仓库地址:https://gitee.com/wwmin/wwm.leetcode.helper
1. 说明
wwm.LeetCodeHelper是一款帮助在本地用C#做LeetCode题的一个库,具有自动拉取题生成csharp文件,自动生成测试用例,自动完成测试对比等等的功能。
- 适用语言范围:C#
- 使用LeetCode版本:国内版
2. 安装
dotnet add package wwm.LeetCodeHelper --version 0.8.6
Install-Package wwm.LeetCodeHelper -Version 0.8.6
3. 使用方式:
创建控制台应用程序 只需要创建一次此控制台应用程序,后面的题库内容都是一个一个类文件 在program.cs文件中添加如下内容
//方式一: 方便自定义刷题
using Microsoft.Extensions.Configuration;
#region 初始化
//添加secrets.json 然后从中读取登录信息
//方法一:
//步骤1: 添加依赖 Microsoft.Extensions.Configuration
//步骤2: 右键项目然后选择管理用户机密,然后在打开的secrets.json中添加
/*
{
"user_password_login": {
"email": "your email",
"password": "your password"
},
"cookie_login": {
"LEETCODE_SESSION": "your leetcode_session cookie",
}
}
*/
// 注意如果项用cookie登录则填充"cookie_login",如果使用用户名密码登录则填充"user_password_login",建议使用cookie,用户名密码模式需要用到模拟登录,且需要下载无头浏览器,配置起来稍微麻烦一些。
//方法二:
//步骤1: 添加依赖 Microsoft.Extensions.Configuration
//步骤2: 启用机密,使用.net cli命令 `dotnet user-secrets init`
//步骤3: 设置机密, `dotnet user-secrets set "cookie_login:LEETCODE_SESSION" "your cookie"`
// 或者设置用户名密码 `dotnet user-secrets set "user_password_login:email" "your email"` `dotnet user-secrets set "user_password_login:password" "your password"`
//步骤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<Program>().Build();
var config_cookie = config.GetRequiredSection("cookie_login:LEETCODE_SESSION").Value;
if (config_cookie != null)
{
await LeetCodeHelper.InitAsync(config_cookie);
}
else
{
var config_email = config.GetRequiredSection("user_password_login:email")?.Value;
var config_password = config.GetRequiredSection("user_password_login:password")?.Value;
if (string.IsNullOrEmpty(config_email) || string.IsNullOrEmpty(config_password))
{
throw new Exception("cookie或者email/password 其中之一是必须的,建议使用cookie");
}
await LeetCodeHelper.InitAsync(config_email, config_password);
}
#endregion
#if !Release //切换此处的开关可在自定义刷题和每日一题之间切换
string url = "";
if (string.IsNullOrEmpty(url))
{
TestResultHelper.InvokeAllTest();
}
else
{
await LeetCodeHelper.GetQuestionAsync(url);
}
#else
//方式二: 方便刷每日一题
await LeetCodeHelper.GetTodayQuestionOrInvokeTestAsync();
#endif
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
只需要将 #if !Release
中Release前加个!即可,这样在Debug模式下系统就执行此处的代码
5.2 每日一题模式
与方式1相反,去掉Release前的!,在Debug模式下系统就执行此处的代码
当然这里的方式1和2的组织模式可以自由发挥。
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
快捷键弹出框中,粘贴文件名后查找到回车即可,此处为了节省文件变多后查找刚下载的文件的时间
内容如下:
namespace Solutions;
/// <summary>
/// 1556. 通过翻转子数组使两个数组相等
/// </summary>
public class CanBeEqualSolution : ITest
{
private class Data : DataAttribute
{
public override IEnumerable<object[]> GetData()
{
yield return new object[] { StringTo<int[]>("[1,2,3,4]").ToArray(), StringTo<int[]>("[2,4,1,3]").ToArray(), true };
yield return new object[] { StringTo<int[]>("[7]").ToArray(), StringTo<int[]>("[7]").ToArray(), true };
yield return new object[] { StringTo<int[]>("[3,7,9]").ToArray(), StringTo<int[]>("[3,7,11]").ToArray(), false };
}
}
[Data]
public bool CanBeEqual(int[] target, int[] arr)
{
return default;
}
}
会在题内容上面加上测试用例, 如果是自定义刷题模式时,将url置空后就会进入执行实现ITest接口的类, 如果是每日刷题,会自动判断题目是否已经下载,如果已下载则进入执行实现ITest的接口类。
7. 运行测试
编写你的算法代码
using wwm.LeetCodeHelper.Servers;
namespace Solutions;
/// <summary>
/// 1. 两数之和
/// https://leetcode.cn/problems/two-sum/
/// </summary>
public class TwoSumSolution : ITest
{
private class Data : DataAttribute
{
public Data()
{
IgnoreOrder = true;
}
public override IEnumerable<object[]> GetData()
{
yield return new object[] { StringTo<int[]>("[2,7,11,15]").ToArray(), 9, StringTo<int[]>("[0,1]").ToArray() };
yield return new object[] { StringTo<int[]>("[3,2,4]").ToArray(), 6, StringTo<int[]>("[1,2]").ToArray() };
yield return new object[] { StringTo<int[]>("[3,3]").ToArray(), 6, StringTo<int[]>("[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
许可证 / License
wwm.LeetCodeHelper 采用 Apache License 2.0 开源许可证。
wwm.LeetCodeHelper uses the Apache License 2.0 open source license.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net6.0
- Microsoft.CodeAnalysis.CSharp (>= 4.2.0)
- Microsoft.Playwright (>= 1.17.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.10.0 | 396 | 2/1/2023 |
0.9.5 | 328 | 12/29/2022 |
0.9.4 | 318 | 12/8/2022 |
0.9.3 | 334 | 12/5/2022 |
0.9.2 | 347 | 12/2/2022 |
0.9.1 | 338 | 12/2/2022 |
0.9.0 | 340 | 12/2/2022 |
0.8.12 | 335 | 12/1/2022 |
0.8.11 | 458 | 9/30/2022 |
0.8.10 | 427 | 9/29/2022 |
0.8.9 | 421 | 9/8/2022 |
0.8.8 | 458 | 8/30/2022 |
0.8.7 | 433 | 8/27/2022 |
0.8.6 | 432 | 8/24/2022 |
0.8.5 | 526 | 2/14/2022 |
0.8.4 | 460 | 2/6/2022 |
0.8.3 | 480 | 2/5/2022 |
0.8.2 | 468 | 2/4/2022 |
0.8.1 | 478 | 2/4/2022 |
0.8.0 | 472 | 2/4/2022 |
0.7.5 | 476 | 1/28/2022 |
0.7.4 | 533 | 1/26/2022 |
0.7.3 | 481 | 1/26/2022 |
0.7.2 | 488 | 1/26/2022 |
0.7.1 | 480 | 1/16/2022 |
0.7.0 | 471 | 1/16/2022 |
0.6.2 | 494 | 1/15/2022 |
0.6.1 | 481 | 1/15/2022 |
0.6.0 | 316 | 1/7/2022 |
0.5.2 | 323 | 12/28/2021 |
0.5.1 | 339 | 12/23/2021 |
0.5.0 | 277 | 12/23/2021 |
0.4.0 | 309 | 12/23/2021 |
0.3.0 | 314 | 12/22/2021 |
0.2.0 | 325 | 12/16/2021 |
0.1.0 | 419 | 9/10/2021 |