代码改变世界

Codlity(2) Min_position_value

  qgbo  阅读(39)  评论(0编辑  收藏  举报
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Min_positive_value
{
    class Min_position_value
    {
        public static int solution(int[] A)
        {
            var positiveA = A.Where(t => t > 0).OrderBy(t => t).ToArray();
 
 
            if (positiveA.Length == 0 || positiveA[0] > 1)
            {
                return 1;
            }
 
            for (int i = 0; i < positiveA.Length; i++)
            {
                if (i + 1 == positiveA.Length)
                    return positiveA[i] + 1;
 
                if (positiveA[i + 1] - positiveA[i] > 1)
                {
                    return positiveA[i] + 1;
                }
            }
 
            throw new Exception("this should not exec");
        }
    }
}

  

努力加载评论中...
点击右上角即可分享
微信分享提示