题目

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 A = [1,1,2],

Your function should return length = 2, and A is now [1,2].

代码:oj测试通过 Runtime: 143 ms

 class Solution:
# @param a list of integers
# @return an integer
def removeDuplicates(self, A):
if len(A) == 0:
return 0 curr = 0
for i in range(0, len(A)):
if A[curr] != A[i] :
A[curr+1],A[i] = A[i],A[curr+1]
curr += 1
return curr+1

思路

首先排除长度为0的special case

使用双指针技巧:用curr指针记录不含有重复元素的数据长度;另一个指针i从前往后走

Tips: 注意curr是数组元素的下标从0开始,所以再最后返回时要返回curr+1

leetcode 【 Remove Duplicates from Sorted Array 】python 实现的更多相关文章

  1. leetcode Remove Duplicates from Sorted Array python

    class Solution(object): def removeDuplicates(self,nums): if len(nums) <= 0: return 0 j=0 for i in ...

  2. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

  3. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  4. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  5. [Leetcode] Remove Duplicates From Sorted Array II (C++)

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

  6. [LeetCode]Remove Duplicates from Sorted Array题解

    Remove Duplicates from Sorted Array: Given a sorted array, remove the duplicates in place such that ...

  7. [Leetcode] Remove duplicates from sorted array ii 从已排序的数组中删除重复元素

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  8. [LeetCode] Remove Duplicates from Sorted Array II [27]

    题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...

  9. [leetcode]Remove Duplicates from Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 题意: Follow up for &quo ...

  10. [LeetCode] Remove Duplicates from Sorted Array

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

随机推荐

  1. 构建第一个spring boot2.0应用之项目启动运行的几种方式(二)

    方法一. 配置Run/Debug Configuration  选择Main Class为项目 Application启动类(入口main方法) (2).进行项目目录,即包含pom.xml的目录下,启 ...

  2. 安装windows phone 7

    本机环境win7 32位旗舰版,本来是4G内存的  系统只能读出2.9G,vs2010中文旗舰版,想搭建windows phone环境学习wp手机开发.安装完了之后明显感觉机器慢了些. ①:安装Mic ...

  3. docker使用centos7系统构建tomcat镜像

    FROM shansongxian/centos-oraclejdk8:latest #此镜像使用centos7系统,精简jdk,只运行java程序,无法编译/构建 MAINTAINER huqian ...

  4. leetcode--3

    1. 题目: Longest Substring Without Repeating Characters Given a string, find the length of the longest ...

  5. 1.VS Code 开发C#入门 安装Dotnet core

    1. dot.net  网站 下载 .NET Core 1.0  (https://www.microsoft.com/net/download/core) 2. 打开命名提示符: 3.dotnet ...

  6. 网络编程——网络模型和InetAddress类

    第1章 网络通信协议 通过计算机网络可以使多台计算机实现连接,位于同一个网络中的计算机在进行连接和通信时需要遵守一定的规则,这就好比在道路中行驶的汽车一定要遵守交通规则一样.在计算机网络中,这些连接和 ...

  7. 分词,复旦nlp,NLPIR汉语分词系统

    http://www.nlpir.org/ http://blog.csdn.net/zhyh1986/article/details/9167593

  8. 转+更新 Graphviz 教程,例子+ 高级应用 写代码,编程绘制架构图(分层拓扑图) 转自官网

    1. Graphviz介绍 Graphviz是大名鼎鼎的贝尔实验室的几位牛人开发的一个画图工具. 它的理念和一般的“所见即所得”的画图工具不一样,是“所想即所得”. Graphviz提供了dot语言来 ...

  9. XML字符串解析

    不多说,直接上代码: import java.io.StringReader; import org.dom4j.Document; import org.dom4j.DocumentExceptio ...

  10. MySQL主从复制原理及配置过程

    一.Mysql数据库的主从复制原理过程: (多实例的安装请参考我的另一篇文章:https://www.cnblogs.com/Template/p/9258500.html) Mysql的主从复制是一 ...