Leetcode 41 缺失的第一个正数 c#

68 篇文章 2 订阅

have a long time not make to leetcode. first trying to hardly type tittle and so eazy to me!

读懂题目以后,分容易:做个从一开始的循环,如果 不包含就输出结果就可以了:

        static void Main(string[] args)
        {
            //int[] t = new int[] { 7,8,9,11,12};
            int res = GetleastNumber(1, 2, 0);
            Console.WriteLine(res);
            Console.ReadKey();  
             
        }

        private static int GetleastNumber(params int[] p)
        {
            int res = 0;
            for (int i = 1; i < 9999; i++)
            {
                if (!p.Contains(i))
                {
                    res = i;
                    break;
                }   
            }
            return res;
        }

如果对9999有疑问的话,可以简单优化:从数组中找到最大的数值+1替换掉9999

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值