程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python description leetcode 80 Delete duplicates in ordered array II

編輯:Python

Python describe LeetCode 80. Remove duplicate items from an ordered array II

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 】.

subject

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

Python describe

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

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved