Hello everyone , I'm Qi Guanjie (qí guān jié ), stay 【 Qi Guanjie 】 official account 、CSDN、GitHub、B Share some technical blog posts on the website and other platforms , It mainly includes front-end development 、python The backend development 、 Applet development 、 Data structure and algorithm 、docker、Linux Common operation and maintenance 、NLP And other related technical blog , Time flies , Future period , come on. ~
If you love bloggers, you can focus on the bloggers' official account. 【 Qi Guanjie 】(qí guān jié), The articles inside are more complete and updated faster . If you need to find a blogger, you can leave a message at the official account. , I will reply to the message as soon as possible .
This article was originally written as 【 Qi Guanjie 】(qí guān jié ), Please support the original , Some platforms have been stealing blog articles maliciously !!! All articles please pay attention to WeChat official account 【 Qi Guanjie 】.
Here's an ordered array nums
, Would you please ** In situ ** Delete duplicate elements , Elements that appear more than twice Only twice , Returns the new length of the deleted array .
Don't use extra array space , You must be there. In situ Modify input array And using O(1) Complete with extra space .
explain :
Why is the return value an integer , But the output answer is array ?
Please note that , The input array is based on **「 quote 」** By way of transmission , This means that modifying the input array in a function is visible to the caller .
You can imagine the internal operation as follows :
// nums In order to “ quote ” By way of transmission . in other words , Do not make any copies of the arguments
int len = removeDuplicates(nums);
// Modifying the input array in a function is visible to the caller .
// Based on the length returned by your function , It prints out the array Within this length All elements of .
for (int i = 0; i < len; i++) {
print(nums[i]);
}
Example 1:
Input :nums = [1,1,1,2,2,3]
Output :5, nums = [1,1,2,2,3]
explain : Function should return the new length length = 5, And the first five elements of the original array are modified to 1, 1, 2, 2, 3 . You don't need to think about the elements in the array that follow the new length .
Example 2:
Input :nums = [0,0,1,1,1,1,2,3,3]
Output :7, nums = [0,0,1,1,2,3,3]
explain : Function should return the new length length = 7, And the first five elements of the original array are modified to 0, 0, 1, 1, 2, 3, 3 . You don't need to think about the elements in the array that follow the new length .
Tips :
1 <= nums.length <= 3 * 104
-104 <= nums[i] <= 104
nums
In ascending order Double pointer , If there are duplicates , Keep the first two , Then walk straight back , Until different numbers
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
n = len(nums)
if n == 1:
return n
l,r = 0,0
while r < n:
nums[l] = nums[r]
l,r = l+1,r+1
if r < n and nums[l-1] == nums[r]:
nums[l] = nums[r]
r += 1
while r < n and nums[r] == nums[l]:
r += 1
l += 1
return l