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

相关推荐
金銀銅鐵6 小时前
[Java] 用 GUI 展示 EnumSet 增减元素时的内部变化(上)
java·后端·ai编程
念何架构之路7 小时前
go-grpc设计思想
开发语言·后端·golang
system_error_x7 小时前
SpringBoot 集成 Thymeleaf 简洁入门
java·spring boot·后端
步行cgn9 小时前
Spring MVC 视图机制的核心接口与类
java·开发语言·后端·spring·mvc
码事漫谈10 小时前
AI 读你的提示词,其实很没耐心
后端
用户83562907805110 小时前
如何使用 Python 格式化 Excel 单元格中的长文本
后端·python
我命由我1234510 小时前
Python 开发 - Python 的 pathlib
java·开发语言·后端·python·学习·java-ee·学习方法
IT_陈寒10 小时前
Java性能优化完全指南
前端·人工智能·后端
电子IT笔记11 小时前
对接群晖NAS,上传和删除踩坑记录
前端·后端
Mk5987811 小时前
P4:把 Agent 的记忆搬进 Redis —— AgentScope-Java 工程化实战
后端