开源项目推荐:MasterNeverDown.LeetCode,刷题必备的C#利器

开源项目推荐:MasterNeverDown.LeetCode,刷题必备的C#利器

各位算法爱好者,今天我要给大家推荐一个超级实用的开源项目------MasterNeverDown.LeetCode!如果你正在用C#刷LeetCode,这个项目绝对能成为你的得力助手,让你的刷题之旅更加顺畅。

项目简介

LeetCode是一个全球知名的算法练习平台,上面有海量的算法题目,是程序员提升算法能力、准备面试的绝佳场所。然而,刷题过程中,我们常常需要重复编写一些基础的数据结构类,比如TreeNode、Node、ListNode等,这不仅浪费时间,还容易出错。MasterNeverDown.LeetCode正是为了解决这个问题而生。

这个项目是一个C#刷题工具包,它为开发者提供了常用的数据结构类和一些实用的方法,帮助你在编写算法题时不必自己创建这些基础类。它还提供了快速单元测试的功能,让你可以快速将object数组初始化为TreeNode对象,与LeetCode无缝对接。此外,它还重写了Equals方法,方便你判断两棵树是否相等。

项目亮点

1. 提供常用数据结构类

项目中包含了TreeNode类、Node类、ListNode类以及ImmutableListNode接口,这些类和接口在LeetCode的很多题目中都会用到。有了这个工具包,你可以直接使用它们,节省大量时间,让你更专注于算法逻辑的实现。

2. 快速单元测试

项目提供了快速单元测试的功能,你可以快速将object数组初始化为TreeNode对象,与LeetCode无缝对接。此外,它还重写了Equals方法,方便你判断两棵树是否相等。

3. 实用方法

项目中还提供了许多实用的方法,比如前序遍历数组转二叉树、返回二叉树的前序、中序、后序遍历等。这些方法在刷题过程中非常实用,可以帮助你快速解决问题。

使用示例

示例1:897. 递增顺序搜索树

这是一个典型的LeetCode题目,题目要求将一个二叉搜索树转换为递增顺序的搜索树。以下是使用MasterNeverDown.LeetCode工具包的解决方案:

csharp 复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using LeetCode.CommunityToolKit.Models;;

namespace LeetCode
{
    /// <summary>
    /// 897. 递增顺序搜索树
    /// https://leetcode.cn/problems/increasing-order-search-tree/  
    /// </summary>
    public class Solution897
    {

        List<int> list = new List<int>();
        Stack<TreeNode> stack = new Stack<TreeNode>();
        public TreeNode IncreasingBST(TreeNode root)
        {
            DFS(root);
            Array.ForEach(list.ToArray(), (x) =>
            {
                if (stack.Count > 0)
                {
                    var node = stack.Peek();
                    var right = new TreeNode(x);
                    node.right = right;
                    stack.Push(right);
                }
                else
                {
                    stack.Push(new TreeNode(x));
                }
            });
            var r = stack.Last();
            return r;
        }

        public void DFS(TreeNode root)
        {
            if (root == null)
                return;

            if (root.left != null)
                DFS(root.left);
            list.Add(root.val);
            if (root.right != null)
                DFS(root.right);
        }
    }
}

示例2:103. 二叉树的锯齿形层次遍历

这是一个稍微复杂一些的题目,要求对二叉树进行锯齿形层次遍历。以下是使用MasterNeverDown.LeetCode工具包的解决方案:

ini 复制代码
namespace LeetCode
{
    public  class Solution103 
    {
        public IList<IList<int>> ZigzagLevelOrder(TreeNode root)
        {
            var res = new List<IList<int>>();
            if (root == null) return res;
            bool isToRight = true;
            var queue = new Queue<TreeNode>();
            queue.Enqueue(root);
            while (queue.Count > 0)
            {
                var count = queue.Count;
                var level = new int[count];
                while (count > 0)
                {
                    var node = queue.Dequeue();
                    level[isToRight ? (level.Length - count) : (count - 1)] = node.val;
                    count--;
                    if (node.left != null)
                    {
                        queue.Enqueue(node.left);
                    }

                    if (node.right != null)
                    {
                        queue.Enqueue(node.right);
                    }
                }
                res.Add(level.ToList());
                isToRight = !isToRight;
            }
            return res;
        }
    }
}

单元测试

项目还提供了方便的单元测试功能,以下是两个示例的单元测试代码:

csharp 复制代码
using LeetCode.CommunityToolKit.Tests;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;

namespace LeetCode.Tests
{
    [TestClass()]
    public class Solution897Tests
    {
        [TestMethod()]
        [DataRow(new object?[] { 5, 3, 6, 2, 4, null, 8, 1, null, null, null, 7, 9 },"", new object?[] { 1, null, 2, null, 3, null, 4, null, 5, null, 6, null, 7, null, 8, null, 9 })]
        public void IncreasingBSTTest(object?[] array,string empty, object?[] exp)
        {
            //empty占位符,否则参数无法匹配
            var expected = exp.CreateTree();
            var node = array.CreateTree();
            //Act
            var actual = new Solution897().IncreasingBST(node);
            //二叉树中序遍历拓展方法
            var a = actual.InorderTraversal();
            //Assert 重写二叉树.Equals方法
            Assert.IsTrue(expected.Equals(actual), "You are wrong!!!");

        }
    }

    [TestClass()]
    public class Solution103Tests
    {
        [TestMethod()]
        [DataRow(new object[]{ 3, 9, 20, null, null, 15, 7 } )]
        public void ZigzagLevelOrderTest(object[] array)
        {
            //Arrange 
            var expected =
                new List<IList<int>> {new List<int>() {3}, new List<int>() {20, 9}, new List<int>() {15, 7}};
            //Act
            var actual = new Solution103().ZigzagLevelOrder(array.CreateTree());

            //Assert
           
            //方法1:拓展方法,对函数返回结果便捷单元测试
            Assert.That.SequenceEqual2(expected, actual, "You are wrong!!!");

        }
    }
}

如何使用

要使用这个工具包,你只需要在你的C#项目中引用它即可。你可以通过NuGet包管理器来安装这个包,安装完成后,就可以直接在你的代码中使用那些预定义的数据结构类和方法了。

升级说明

以下是项目的升级历史:

版本 新增功能 日期
4.1.4 Node实例相等断言 2022-07-14
4.2.0 ImmutableListNode接口 2025-03-31

总结

MasterNeverDown.LeetCode是一个非常实用的C#刷题工具包,它为开发者提供了常用的数据结构类和一些实用的方法,帮助你在编写算法题时不必自己创建这些基础类。它还提供了快速单元测试的功能,让你可以快速将object数组初始化为TreeNode对象,与LeetCode无缝对接。此外,它还重写了Equals方法,方便你判断两棵树是否相等。如果你正在用C#刷LeetCode,那么这个工具包绝对值得一试。快去NuGet上搜索并安装它吧,让你的刷题之旅更加顺畅!

相关推荐
派大鑫wink10 分钟前
【JAVA学习日志】SpringBoot 参数配置:从基础到实战,解锁灵活配置新姿势
java·spring boot·后端
程序员爱钓鱼28 分钟前
Node.js 编程实战:文件读写操作
前端·后端·node.js
xUxIAOrUIII33 分钟前
【Spring Boot】控制器Controller方法
java·spring boot·后端
Dolphin_Home36 分钟前
从理论到实战:图结构在仓库关联业务中的落地(小白→中级,附完整代码)
java·spring boot·后端·spring cloud·database·广度优先·图搜索算法
zfj32138 分钟前
go为什么设计成源码依赖,而不是二进制依赖
开发语言·后端·golang
weixin_462446231 小时前
使用 Go 实现 SSE 流式推送 + 打字机效果(模拟 Coze Chat)
开发语言·后端·golang
JIngJaneIL1 小时前
基于springboot + vue古城景区管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
小信啊啊2 小时前
Go语言切片slice
开发语言·后端·golang
Victor3563 小时前
Netty(20)如何实现基于Netty的WebSocket服务器?
后端
缘不易3 小时前
Springboot 整合JustAuth实现gitee授权登录
spring boot·后端·gitee