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         }
  • 相关阅读:
    Linux修改root密码报错
    Python中的排序---冒泡法
    Python随机数
    Python中的深拷贝和浅拷贝
    Couldn’t download https://raw.githubusercontent.com/certbot/certbot/ 问题解决
    Python内置数据结构----list
    Python内置数据结构
    Vue指令
    computed 和 watch
    Vue的数据响应式
  • 原文地址:https://www.cnblogs.com/HuoAA/p/4672185.html
Copyright © 2011-2022 走看看