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.

#-*- coding:utf-8 -*-

class Solution:
# @param {integer[]} nums
# @return {integer}
def removeDuplicates(self, nums):
l = len(nums)
if l == 0:
return 0
index = 0
i = 1
nums[index] = nums[0]
while i<l:
if nums[index] != nums[i]:
index += 1
nums[index] = nums[i]
i += 1
return index+1 if __name__=="__main__":
s = Solution()
print s.removeDuplicates(x)

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][Python]26: Remove Duplicates from Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...

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

    Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...

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

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

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

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

  6. Remove Duplicates From Sorted Array

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

  7. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  8. 26. Remove Duplicates from Sorted Array

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

  9. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

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

随机推荐

  1. 如何解决Linux下的软件包依赖问题

    650) this.width=650;" border="0" alt="" src="http://img1.51cto.com/att ...

  2. 【习题 8-8 UVA - 1612】Guess

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] double千万不要用==判断相等... 而且两个保留2位有效数字的数字x,y 判断它们相等应该这样. int temp1 = ro ...

  3. Oracle11gR2 Windows 7 64bit and PL/SQL Developer排错

    1.Environment variable: "PATH" - This test checks whether the length of the environment va ...

  4. D3.js加载csv和json数据

    1.加载数据的基本命令 D3提供了方法可以对不同的数据类型进行加载,比如d3.text(), d3.xml(), d3.json(), d3.csv(), 和d3.html(). <!DOCTY ...

  5. MD5和sha1加密算法--散列加密技术 MD5:128bit的大整数

    在很多电子商务和社区应用中,我们都要存放很多的客户的资料,其中包括了很多的隐私信息和客户不愿被别人看到的信息,当然好有客户执行各种操作的密码,此时就需要对客户的信息进行加密再存储,目前有两种比较好的加 ...

  6. canvas:飞机大战

    最开始我们要初始化信息,我们有五个状态,游戏封面,加载状态,运行状态,游戏暂停,游戏结束 我们还需要得分score,生命life var START = 1;//初始状态 var LOADING = ...

  7. PYTHON学习第五天课后总结:

    今日重点: 数字类型 字符串类型 列表类型 元组类型 1,数字类型    数字类型为不可变类型   也只能将纯数字类型的字符串转换成int包括: 整型: int()     int() 为内置函数,可 ...

  8. 【2017 Multi-University Training Contest - Team 6】Kirinriki

    [链接]http://acm.hdu.edu.cn/showproblem.php?pid=6103 [题意] 给出一串字符串,从中选出两个不重叠的字符串,使得两个字符串的距离和 <= m 的最 ...

  9. 【MySQL集群】——Java程序连接MySQL集群

    上篇简介了怎样在Windows环境下建立配置MySQL集群,这里用一个实现注冊功能的小Demo通过jdbc的方式连接到MySQL集群中. 外部程序想要远程连接到mysql集群,还须要做的一个操作就是设 ...

  10. Android 使用Wake Lock

    为了延长电池的使用寿命,Android设备会在一段时间后使屏幕变暗,然后关闭屏幕显示,最后停止CPU.WakeLock是一个电源管理系统服务功能,应用程序可以使用它来控制设备的电源状态. WakeLo ...