当前位置:网站首页>Leetcode 26. delete duplicates in the ordered array
Leetcode 26. delete duplicates in the ordered array
2022-07-29 06:22:00 【Zhangchuming ZCM】
Power button | 26. Remove duplicate items from an ordered array
Title screenshot

Be careful : The topic requires that no extra space be occupied , It is not allowed to modify the input array in place .
Method : Double finger needling
Ideas : Compare the elements in the array with each other , If you find duplicates, delete . But this method is generally applicable to creating a new empty array , Put every non repeating element into it . If you modify it directly in the original array , Misoperation may occur . Better think in reverse . Use two cursors to indicate , A cursor is responsible for recording changes , The other is for comparison . Compare the cursors. If the values before and after are different, use the recording cursor to record . This ensures that the new array will not have duplicate options . Return the recorded value .
class Solution:
def removeDuplicates(self, nums: List[int]) -> int:
if not nums:
return 0
n = len(nums)
i = 1
j = 1
while j < n:
if nums[j] != nums[j - 1]:
nums[i] = nums[j]
i += 1
j += 1
return i
if __name__ == "__main__":
a = Solution()
nums = [1, 1, 2]
print(a.removeDuplicates(nums))
边栏推荐
- Huawei cloud 14 day Hongmeng device development -day5 drive subsystem development
- FPGA based: moving target detection (schematic + source code + hardware selection, available)
- Encapsulation - Super keyword
- 八大排序-----------快速排序
- [beauty of software engineering - column notes] 16 | how to write project documents?
- 循环链表和双向链表
- 太原市公交路线爬取
- Design and implementation of QT learning notes data management system
- Joiner.on和stream().map联合使用技巧
- 【软件工程之美 - 专栏笔记】13 | 白天开会,加班写代码的节奏怎么破?
猜你喜欢

Logistic regression - project practice - credit card detection task (Part 2)

Sqlyog installation and configuration tutorial

Based on STM32: couple interactive doll (design scheme + source code +3d drawing +ad circuit)

传统模型预测控制轨迹跟踪——波浪形轨迹(功能包已经更新)

STM32 检测信号频率

【软件工程之美 - 专栏笔记】23 | 架构师:不想当架构师的程序员不是好程序员

Eight sorts --------- quick sort

SQLyog 安装和配置教程

Huawei cloud 14 days Hongmeng device development -day1 environment construction

JUC concurrent knowledge points
随机推荐
Eight sorts ----------- bubble sort
Huawei cloud 14 day Hongmeng device development -day2 compilation framework
Multithreading and concurrency
synchronized八锁现象理解
【软件工程之美 - 专栏笔记】28 | 软件工程师的核心竞争力是什么?(下)
LeetCode #283.移动零
leetcode刷题笔记 763.划分字母区间(中等)
一些工具,插件,软件链接分享给大家~
Huawei cloud 14 day Hongmeng device development -day5 drive subsystem development
【Leetcode刷题】数组1——双指针
【软件工程之美 - 专栏笔记】21 | 架构设计:普通程序员也能实现复杂系统?
STM32FF030 替代国产单片机——DP32G030
LeetCode #13. 罗马数字转整数
DP1332E多协议高度集成非接触式读写芯片
LeetCode #19.删除链表的倒数第N个结点
Understanding of synchronized eight lock phenomenon
【软件工程之美 - 专栏笔记】30 | 用好源代码管理工具,让你的协作更高效
Eight sorts --------- quick sort
八大排序-----------------堆排序
LeetCode #1.两数之和