开源项目推荐: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上搜索并安装它吧,让你的刷题之旅更加顺畅!

相关推荐
佚名涙34 分钟前
go中锁的入门到进阶使用
开发语言·后端·golang
草捏子6 小时前
从CPU原理看:为什么你的代码会让CPU"原地爆炸"?
后端·cpu
嘟嘟MD6 小时前
程序员副业 | 2025年3月复盘
后端·创业
胡图蛋.6 小时前
Spring Boot 支持哪些日志框架?推荐和默认的日志框架是哪个?
java·spring boot·后端
无责任此方_修行中6 小时前
关于 Node.js 原生支持 TypeScript 的总结
后端·typescript·node.js
吃海鲜的骆驼7 小时前
SpringBoot详细教程(持续更新中...)
java·spring boot·后端
迷雾骑士7 小时前
SpringBoot中WebMvcConfigurer注册多个拦截器(addInterceptors)时的顺序问题(二)
java·spring boot·后端·interceptor
uhakadotcom8 小时前
Thrift2: HBase 多语言访问的利器
后端·面试·github
Asthenia04128 小时前
Java 类加载规则深度解析:从双亲委派到 JDBC 与 Tomcat 的突破
后端
方圆想当图灵8 小时前
从 Java 到 Go:面向对象的巨人与云原生的轻骑兵
后端·代码规范