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

相关推荐
zopple3 小时前
常见的 Spring 项目目录结构
java·后端·spring
cjy0001115 小时前
springboot的 nacos 配置获取不到导致启动失败及日志不输出问题
java·spring boot·后端
小江的记录本6 小时前
【事务】Spring Framework核心——事务管理:ACID特性、隔离级别、传播行为、@Transactional底层原理、失效场景
java·数据库·分布式·后端·sql·spring·面试
sheji34166 小时前
【开题答辩全过程】以 基于springboot的校园失物招领系统为例,包含答辩的问题和答案
java·spring boot·后端
程序员cxuan6 小时前
人麻了,谁把我 ssh 干没了
人工智能·后端·程序员
wuyikeer7 小时前
Spring Framework 中文官方文档
java·后端·spring
Victor3567 小时前
MongoDB(61)如何避免大文档带来的性能问题?
后端
Victor3567 小时前
MongoDB(62)如何避免锁定问题?
后端
wuyikeer8 小时前
Spring BOOT 启动参数
java·spring boot·后端
子木HAPPY阳VIP9 小时前
Ubuntu 22.04 VMware 设置固定IP配置
人工智能·后端·目标检测·机器学习·目标跟踪