leetcode70,【leetcode】Remove Duplicates from Sorted Array

 2023-10-08 阅读 26 评论 0

摘要:題目:Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. leetcode70。 For example, Gi

題目:Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

leetcode70。 For example,
Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length

?

leetcode-cn,本題在leetcode中屬于esay的級別,也確實不難。

遍歷list,用兩個變量記錄當前位置和下一位置的值,如果兩個值相等,刪掉下一位置元素,不相等就兩個變量均往后移一位。

class Solution(object):def removeDuplicates(self, nums):""":type nums: List[int]:rtype: int"""if len(nums) == 0:return 0elif len(nums) == 1:return 1current = 0next = 1while True:if next >= len(nums):breakif nums[current] == nums[next]:nums.pop(next)else:temp = nextnext = next + 1current = tempreturn len(nums)

?

leetcode最長無重復字符串。轉載于:https://www.cnblogs.com/seyjs/p/5222882.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/131873.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息