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 }