fueanta

LeetCode 27: Remove Element.

Mar 24th, 2020
162
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Given an array nums and a value val, remove all instances of that value in-place and
  3. return the new length. Do not allocate extra space for another array, you must do this by
  4. modifying the input array in-place with O(1) extra memory. The order of elements can be
  5. changed. It doesn't matter what you leave beyond the new length.
  6.  
  7. Examples:
  8. ---------
  9. Given nums = [3,2,2,3], val = 3,
  10.  
  11. Your function should return length = 2, with the first two elements of nums being 2.
  12.  
  13. It doesn't matter what you leave beyond the returned length.
  14. -----------------------------------
  15. Given nums = [0,1,2,2,3,0,4,2], val = 2,
  16.  
  17. Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.
  18.  
  19. Note that the order of those five elements can be arbitrary.
  20.  
  21. It doesn't matter what values are set beyond the returned length.
  22.  
  23. Clarification:
  24. --------------
  25. Confused why the returned value is an integer but your answer is an array?
  26.  
  27. Note that the input array is passed in by reference, which means modification to the input
  28. array will be known to the caller as well. Internally you can think of this:Confused why the
  29. returned value is an integer but your answer is an array?
  30.  
  31. Note that the input array is passed in by reference, which means modification to the input
  32. array will be known to the caller as well. Internally you can think of this:
  33.  
  34. // nums is passed in by reference. (i.e., without making a copy)
  35. int len = removeElement(nums, val);
  36.  
  37. // any modification to nums in your function would be known by the caller.
  38. // using the length returned by your function, it prints the first len elements.
  39. for (int i = 0; i < len; i++) {
  40.     print(nums[i]);
  41. }
  42. */
  43.  
  44. namespace Problems.LeetCode.Array.RemoveElement_27
  45. {
  46.     public class Solution
  47.     {
  48.         /// <summary>
  49.         ///     Removes all the instances of a given value from an array.
  50.         ///     Uses two pointers. Does not maintain order.
  51.         ///     Two pointers can treverse upto n times.
  52.         ///     Time complexity : O(n)
  53.         ///     Space complexity : O(1)
  54.         /// </summary>
  55.         /// <param name="nums">An array of integers.</param>
  56.         /// <param name="val">An integer.</param>
  57.         public int RemoveElement(int[] nums, int val)
  58.         {
  59.             if (nums.Length == 0) return 0;
  60.  
  61.             var length = nums.Length;
  62.  
  63.             for (var i = 0; i < length;)
  64.                 if (nums[i] == val) nums[i] = nums[--length];
  65.                 else i++;
  66.  
  67.             return length;
  68.         }
  69.  
  70.         /// <summary>
  71.         ///     Removes all the instances of a given value from an array.
  72.         ///     Uses two pointers. Maintains order.
  73.         ///     Two pointers can treverse upto 2n times.
  74.         ///     Time complexity : O(n)
  75.         ///     Space complexity : O(1)
  76.         /// </summary>
  77.         /// <param name="nums">An array of integers.</param>
  78.         /// <param name="val">An integer.</param>
  79.         public int RemoveElement2(int[] nums, int val)
  80.         {
  81.             if (nums.Length == 0) return 0;
  82.  
  83.             var length = 0;
  84.  
  85.             for (var i = 0; i < nums.Length; i++)
  86.                 if (nums[i] != val) nums[length++] = nums[i];
  87.  
  88.             return length;
  89.         }
  90.     }
  91. }
RAW Paste Data