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

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

你可能感兴趣的:(c#,Leetcode)