Advertisement
fueanta

LeetCode 287: Find the Duplicate Number.

Mar 24th, 2020
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 KB | None | 0 0
  1. /*
  2. Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive),
  3. prove that at least one duplicate number must exist. Assume that there is only one duplicate number,
  4. find the duplicate one.
  5.  
  6. Example 1:
  7. ----------
  8. Input: [1,3,4,2,2]
  9. Output: 2
  10.  
  11. Example 2:
  12. ----------
  13. Input: [3,1,3,4,2]
  14. Output: 3
  15.  
  16. Note:
  17. -----
  18. 1. You must not modify the array (assume the array is read only).
  19. 2. You must use only constant, O(1) extra space.
  20. 3. Your runtime complexity should be less than O(n2).
  21. 4. There is only one duplicate number in the array, but it could be repeated more than once.
  22. */
  23.  
  24. namespace CsharpSolutions.Problems.LeetCode.Array.FindTheDuplicateNumber_287
  25. {
  26.     public class Solution
  27.     {
  28.         /// <summary>
  29.         ///     Finds duplicate in an array of integers.
  30.         ///     Uses Floyds Tortoise and Hare Algorithm.
  31.         ///     Time complexity : O(n)
  32.         ///     Space complexity : O(1)
  33.         /// </summary>
  34.         /// <param name="nums">An array of integers.</param>
  35.         public int FindDuplicate(int[] nums)
  36.         {
  37.             var tortoise = nums[nums[0]];
  38.             var hare = nums[nums[nums[0]]];
  39.  
  40.             while (tortoise != hare)
  41.             {
  42.                 tortoise = nums[tortoise];
  43.                 hare = nums[nums[hare]];
  44.             }
  45.  
  46.             hare = nums[0];
  47.  
  48.             while (tortoise != hare)
  49.             {
  50.                 tortoise = nums[tortoise];
  51.                 hare = nums[hare];
  52.             }
  53.  
  54.             return hare;
  55.         }
  56.  
  57.         /// <summary>
  58.         ///     Finds duplicate in an array of integers.
  59.         ///     Uses HashSet to record previously seen integers.
  60.         ///     Time complexity : O(n)
  61.         ///     Space complexity : O(n)
  62.         /// </summary>
  63.         /// <param name="nums">An array of integers.</param>
  64.         public int FindDuplicate2(int[] nums)
  65.         {
  66.             var seenIntegers = new System.Collections.Generic.HashSet<int>();
  67.  
  68.             foreach (var num in nums)
  69.             {
  70.                 if (seenIntegers.Contains(num))
  71.                     return num;
  72.                 seenIntegers.Add(num);
  73.             }
  74.  
  75.             return -1;
  76.         }
  77.  
  78.         /// <summary>
  79.         ///     Finds duplicate in an array of integers.
  80.         ///     Sorts the array first, then applies linear search.
  81.         ///     Time complexity : O(nlgn)
  82.         ///     Space complexity : If source is mutable, O(1) / Otherwise, O(n)
  83.         /// </summary>
  84.         /// <param name="nums">An array of integers.</param>
  85.         public int FindDuplicate3(int[] nums)
  86.         {
  87.             System.Array.Sort(nums);
  88.  
  89.             for (var i = 1; i < nums.Length; i++)
  90.                 if (nums[i] == nums[i - 1])
  91.                     return nums[i];
  92.  
  93.             return -1;
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement