当前位置:网站首页>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))
边栏推荐
- Ml self study notes 5
- LeetCode #977.有序数组的平方
- Rowkey设计
- leetcode刷题笔记 452. Minimum Number of Arrows to Burst Balloons (Medium) 452.用最少数量的箭引爆气球(中等)
- 关于【链式前向星】的自学理解
- markdown与Typora
- Power electronics: single inverter design (matlab program +ad schematic diagram)
- 基于F407ZGT6的WS2812B彩灯驱动
- Markdown and typora
- Ml6 self study notes
猜你喜欢
随机推荐
唯美girls
【Leetcode刷题】数组1——双指针
EPS32+Platform+Arduino 跑马灯
【软件工程之美 - 专栏笔记】25 | 有哪些方法可以提高开发效率?
Ml4 self study notes
LeetCode #19.删除链表的倒数第N个结点
FPGA based: moving target detection (supplementary simulation results, available)
[beauty of software engineering - column notes] 19 | as a programmer, you should have product awareness
Maya ACES工作流程配置(Arnold 及 RedShift 贴图配置规范-还原出SP-Aces流程下贴图正确的效果) PS还原Aces流程下渲染的图
LeetCode #557.反转字符串中的单词 III
Mathematical modeling experience
2022 spring move - core technology FPGA post technical aspects (one side experience)
利用云打码来破解登录遇到验证码的问题
shell工具finalShell
Markdown and typora
一些工具,插件,软件链接分享给大家~
JUC集合类不安全
【软件工程之美 - 专栏笔记】13 | 白天开会,加班写代码的节奏怎么破?
【软件工程之美 - 专栏笔记】27 | 软件工程师的核心竞争力是什么?(上)
SimpleFOC调参1-力矩控制