The title

LeetCode 第 26 题, Remove duplicate association type from sorted array: array

Give you an ordered array nums, ask you to delete the repeated elements in place, so that each element appears only once, return the deleted array after the new length. Instead of using extra array space, you must modify the input array in place and do so with O(1) extra space. Explanation: Why is the return value an integer, but the output answer is an array? Note that the input array is passed "by reference," which means that modifying the input array in a function is visible to the caller. You can imagine the internal operation as follows: // NUMs is passed "by reference". That is, it does not make any copies of the arguments int len = removeDuplicates(nums); // Modifying the input array in a function is visible to the caller. // Depending on the length returned by your function, it prints out all elements in the array within that length. for (int i = 0; i < len; i++) { print(nums[i]); } Example 1: Input: nums = [1,1,2] Output: 2, nums = [1,2] Explanation: The function should return a new length of 2, and the first two elements of the original array nums are changed to 1,2. You don't need to worry about the element after the new length in the array. Example 2: Input: nums = [0,0,1,1, 2,2,3,3,4] Output: 5, nums = [0,1,2, 2,3,4] Explanation: The function should return a new length of 5, and the first five elements of the original nums array are modified to 0,1,2,3,4. You don't need to worry about the element after the new length in the array. 0 <= nums.length <= 3 * 104-104 <= nums[I] <= 104 NUMs are sorted in ascending orderCopy the code

Time to solve the problem.

class Solution {
    public int removeDuplicates(int[] nums) {
       
       
       
    }
}
Copy the code

The method input parameters are given above to complete the answer.

Subject analysis

  1. So the first thing to notice is a couple of points
  • Instead of using extra array space, you must modify the input array in place and do so with O(1) extra space
  • Input: nums = [0,0,1,1, 2,2,3,3,4]

Output: 5, nums = [0,1,2,3,4] Explanation: The function should return a new length of 5, and the first five elements of the original array nums are modified to 0,1,2,3,4. You don’t need to worry about the element after the new length in the array. 2. We can use two Pointers, one for comparison, one for recording the insertion position, when two different cases are encountered, assign it to the insertion position, and finally return the array length.

Answers to analysis

This article only analysis I do the idea, only for reference, to understand a solution to the idea, other kinds of ideas to do the problem please access the Internet.

Answer successful: Execution time :1 ms, beat 80.82% Java users memory consumption :40.3 MB, beat 44.02% Java users

class Solution {
    public int removeDuplicates(int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        int i = 0;
        for (int j = 0; j < nums.length - 1; j++) {
            if (nums[j] != nums[j + 1]) {
                i++;
                nums[i]=nums[j+1];
            }
        }
        return i+1;
    }
}
Copy the code