博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
leetcode26. [Array]Remove Duplicates from Sorted Array
阅读量:4221 次
发布时间:2019-05-26

本文共 896 字,大约阅读时间需要 2 分钟。

Total Accepted: 117053 Total Submissions: 354699 Difficulty: Easy

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.

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.

class Solution(object):    def removeDuplicates(self, nums):        j=1;count=0        if not len(nums):            return 0        tmp=nums[0]        for i in range(1,len(nums)):            if nums[i]!=tmp:                nums[j]=nums[i]                j=j+1                tmp=nums[i]            else:                count=count+1        for i in range(count):            nums.pop()        return len(nums)

转载地址:http://kzqmi.baihongyu.com/

你可能感兴趣的文章
Python邮件发送
查看>>
Ajax请求下,sendRedirect无效的问题
查看>>
python数据类型(python cookbook读书笔记一)
查看>>
python cookbook读书笔记二
查看>>
VS添加第三方C/C++库经验
查看>>
无法定位序数55于动态链接库zlib1.dll上
查看>>
sqlalchemy 这原生sql中绑定list
查看>>
rust所有权理解(备忘)
查看>>
Java使用redis+sse实现带频道的网络聊天室
查看>>
deepin下安装docker-ce
查看>>
深入理解java虚拟机读书笔记——基础知识篇
查看>>
深入java虚拟机读书笔记——类加载与方法调用中的分派机制
查看>>
面试中遇到的有趣的小问题
查看>>
akka分布式爬虫框架(一)——设计思路与demo
查看>>
docker搭建kafka
查看>>
spring的webflux初探
查看>>
Mesos入门(一)——白皮书阅读
查看>>
mesos入门(二)——简单环境搭建
查看>>
mesos入门(三)—— HA模式
查看>>
mesos入门(四)——docker应用的部署
查看>>