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         }
  • 相关阅读:
    2-SAT
    模板 两次dfs
    SG函数与SG定理
    NIM博弈
    python 给小孩起名
    pytest 数据驱动
    pytest 结合selenium 运用案例
    字符串的转换方法与分割
    字符串的方法
    字符串常量池与字符串之间的比较
  • 原文地址:https://www.cnblogs.com/HuoAA/p/4672185.html
Copyright © 2011-2022 走看看