Algorithm leetcode|80. Remove duplicates in ordered array II (rust hits hard)



80. Remove duplicates in ordered array II:

You are given an ordered array nums, please delete the repeated elements in place , so that the elements that appear more than twice only appear twice, and return the new length of the array after deletion.

Instead of using extra array space, you must modify the input array in -place and do it using O(1) extra space.

Description :

Why is the returned value an integer but the output answer is an array?

Please note that the input array is passed by " reference ", which means that modifications to the input array in the function are visible to the caller.

You can imagine the internal operation as follows:

// nums 是以“引用”方式传递的。也就是说,不对实参做任何拷贝
int len = removeDuplicates(nums);

// 在函数里修改输入数组对于调用者是可见的。
// 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
for (int i = 0; i < len; i++) {
    print(nums[i]);
}

Example 1:

输入:
	
	nums = [1,1,1,2,2,3]
	
输出:
	
	5, nums = [1,1,2,2,3]
	
解释:
	
	函数应返回新长度 length = 5, 并且原数组的前五个元素被修改为 1, 1, 2, 2, 3。 不需要考虑数组中超出新长度后面的元素。

Example 2:

输入:
	
	nums = [0,0,1,1,1,1,2,3,3]
	
输出:
	
	7, nums = [0,0,1,1,2,3,3]
	
解释:
	
	函数应返回新长度 length = 7, 并且原数组的前五个元素被修改为 0, 0, 1, 1, 2, 3, 3。不需要考虑数组中超出新长度后面的元素。

hint:

  • 1 <= nums.length <= 3 * 104
  • -104 <= nums[i] <= 104
  • nums is sorted in ascending order

analyze:

  • Facing this algorithm question, the second master once again fell into deep thought.
  • This question is very similar to 26. Delete duplicates in an ordered array . The difference is that this question allows the same element to be repeated at most once.
  • If you want to delete duplicates, it is a more intuitive idea to use data structures such as mapping tables to count. However, the question requires that only constant-level extra space can be used, so you cannot rely on equipment but skills.
  • There is a very important piece of information in the question, that is, order, which means that the same numbers will be next to each other. That is, the current subscript and the previous subscript are allowed to have the same number at most. Judge the current subscript and the previous subscript. You can quickly determine duplication if the marked numbers are the same, that is, if nums[i]and nums[i - 2]are the same, the element must be deleted.
  • Since the requirement is to modify it in place, we need the length of a new array (the return value itself also requires the length of the new array), and we also need a traversal subscript, that is, a double pointer, which only needs to be traversed once, and only requires constant-level extra space and very efficient.
  • In addition, if the array elements are less than 3 (or the array length is less than/equal to 2), it definitely meets the meaning of the question and can be returned directly without any processing.

answer:

rust:

impl Solution {
    
    
    pub fn remove_duplicates(nums: &mut Vec<i32>) -> i32 {
    
    
        let n = nums.len();
        if n <= 2 {
    
    
            return n as i32;
        }
        let (mut slow, mut fast) = (2, 2);
        while fast < n {
    
    
            if nums[slow - 2] != nums[fast] {
    
    
                nums[slow] = nums[fast];
                slow += 1;
            }
            fast += 1;
        }
        return slow as i32;
    }
}

go:

func removeDuplicates(nums []int) int {
    
    
	n := len(nums)
	if n <= 2 {
    
    
		return n
	}
	slow, fast := 2, 2
	for fast < n {
    
    
		if nums[slow-2] != nums[fast] {
    
    
			nums[slow] = nums[fast]
			slow++
		}
		fast++
	}
	return slow
}

c++:

class Solution {
    
    
public:
    int removeDuplicates(vector<int>& nums) {
    
    
        const int n = nums.size();
        if (n <= 2) {
    
    
            return n;
        }
        int slow = 2, fast = 2;
        while (fast < n) {
    
    
            if (nums[slow - 2] != nums[fast]) {
    
    
                nums[slow] = nums[fast];
                ++slow;
            }
            ++fast;
        }
        return slow;
    }
};

python:

class Solution:
    def removeDuplicates(self, nums: List[int]) -> int:
        slow = 2
        for fast in range(2, len(nums)):
            if nums[fast] != nums[slow - 2]:
                nums[slow] = nums[fast]
                slow += 1
        return slow


java:

class Solution {
    
    
    public int removeDuplicates(int[] nums) {
    
    
        final int n = nums.length;
        if (n <= 2) {
    
    
            return n;
        }
        int slow = 2, fast = 2;
        while (fast < n) {
    
    
            if (nums[slow - 2] != nums[fast]) {
    
    
                nums[slow] = nums[fast];
                ++slow;
            }
            ++fast;
        }
        return slow;
    }
}

Thank you very much for reading this article~
Welcome to [Like] [Collect] [Comment] Three in a row ~ It
is not difficult to give up, but it must be cool to persist ~
I hope we can all make a little progress every day ~
This article is written by the second-in-command white hat: https://le-yi.blog.csdn.net/Original blog~


Guess you like

Origin blog.csdn.net/leyi520/article/details/132802590