当前位置:网站首页>LeetCode26: remove duplicates in sorted array

LeetCode26: remove duplicates in sorted array

2022-08-09 23:11:00 Doraemon 0219

Given you an array nums in ascending order, please delete the repeated elements in place, so that each element appears only once, and return the new length of the deleted array.The relative order of elements should remain consistent.

Because the length of the array cannot be changed in some languages, the result must be placed in the first part of the array nums.More canonically, if there are k elements after removing duplicates, then the first k elements of nums should hold the final result.

Return k after inserting the final result into the first k positions of nums .

Don't use extra space, you have to modify the input array in-place and do it with O(1) extra space.

Source: LeetCode
Link: https://leetcode.cn/problems/remove-duplicates-from-sorted-array

This topic adopts the double-pointer method. It is very interesting to see such a passage in the Likou comment area, which can help us understand this double-pointer method:

Double pointer method (C++)

Double pointer method is very useful in array and linked list problems.

class Solution {public:int removeDuplicates(vector&nums){int fast = 1;//fast pointerint slow = 0;//Slow pointerif(nums.size()==0)return 0;for(fast = 1;fast

原网站

版权声明
本文为[Doraemon 0219]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208091954448728.html