zoukankan      html  css  js  c++  java
  • PermMissingElem

     1         /// <summary>
     2         /// Solution A
     3         /// 100/100
     4         /// </summary>
     5         /// <param name="A"></param>
     6         /// <returns></returns>
     7         public static int solution(int[] A)
     8         {
     9             HashSet<int> hs = new HashSet<int>();
    10 
    11             foreach (int i in A)
    12             {
    13                 hs.Add(i);
    14             }
    15 
    16             for (int i = 1; i <= A.Length + 1; i++)
    17             {
    18                 if (!hs.Contains(i))
    19                     return i;
    20             }
    21             return 0;
    22         }
    23 
    24         /// <summary>
    25         /// Solution B
    26         /// 100/100
    27         /// </summary>
    28         /// <param name="A"></param>
    29         /// <returns></returns>
    30         public static int solution(int[] A)
    31         {
    32 
    33             //int N = A.Length;
    34             long N = A.Length;
    35 
    36             //总和可能超出int最大值,所以用long
    37             long sumFull = (N + 1) * (N + 2) / 2;
    38 
    39             //long sumLack = A.Sum();
    40             long sumLack = 0;
    41 
    42             foreach (int i in A)
    43                 sumLack += i;
    44 
    45             return (int)(sumFull - sumLack);
    46         }
    47 
    48         /// <summary>
    49         /// Solution C
    50         /// 100/100
    51         /// </summary>
    52         /// <param name="A"></param>
    53         /// <returns></returns>
    54         public static int solution(int[] A)
    55         {
    56             Array.Sort(A);
    57             int N = A.Length;
    58             for (int i = 1; i <= N; i++)
    59             {
    60                 if (A[i - 1] != i)
    61                     return i;
    62             }
    63             return N + 1;
    64         }
  • 相关阅读:
    在CentOS 7上安装Docker
    VMware虚拟机上安装CentOS 7
    5、Linux的常用命令
    3.Linux 系统目录结构
    2、Linux的关机方式
    zepto callback
    解callback嵌套
    debugger 调试的一些经验
    Chrome Timeline的指标说明:Blocked、Connect、Send、Wait、Receive
    jquery ajax promise
  • 原文地址:https://www.cnblogs.com/HuoAA/p/4672185.html
Copyright © 2011-2022 走看看