# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array
https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ 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]. ===Comments by Dabay===
一次循环,两个指针,一个指向最后插入的位置,另外一个一直往前面走。
如果两个指针的数一样,二号指针继续走。
如果不一样,把二号指针指向的数插入到一号指针的后面。
最后跟新数组,返回长度。
''' class Solution:
# @param a list of integers
# @return an integer
def removeDuplicates(self, A):
i,j = 0,0
while j < len(A):
if A[i] != A[j]:
i += 1
A[i] = A[j]
j += 1
A = A[:i+1]
return len(A) def main():
sol = Solution()
print sol.removeDuplicates([1,1,2]) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[Leetcode][Python]26: Remove Duplicates from Sorted Array的更多相关文章

  1. C# 写 LeetCode easy #26 Remove Duplicates from Sorted Array

    26.Remove Duplicates from Sorted Array Given a sorted array nums, remove the duplicates in-place suc ...

  2. 【leetcode】 26. Remove Duplicates from Sorted Array

    @requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...

  3. 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...

  4. 【LeetCode】26. 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】#26. Remove Duplicates from Sorted Array

    一天一道LeetCode系列 (一)题目 Given a sorted array, remove the duplicates in place such that each element app ...

  6. LeetCode OJ 26. Remove Duplicates from Sorted Array

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

  7. Leetcode No.26 Remove Duplicates from Sorted Array(c++实现)

    1. 题目 1.1 英文题目 Given an integer array nums sorted in non-decreasing order, remove the duplicates in- ...

  8. LeetCode:26. Remove Duplicates from Sorted Array(Easy)

    1. 原题链接 https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ 2. 题目要求 给定一个已 ...

  9. 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

随机推荐

  1. Android 圆形背景shape定义

    <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http: ...

  2. JS基础如何理解对象

    这几天跟几个同事聊天发现他们对javascript什么时候该用new都不是很了解. 1.javascript的function什么时候该new什么时候不该new?我觉得主要的问题还是集中在javasc ...

  3. lua学习笔记(2)-常用调用

    assert(loadstring("math.max(7,8,9)"))dofile("scripts/xxx.lua")math.floor()math.r ...

  4. 异步方式向WPF ListBox控件中一条一条添加记录

    向ListBox绑定数据源时,如果数据量过大,可能会使得程序卡死,这是就需要一条一条的向ListBox的数据源中添加记录了,下面是个小Demo: 1.前台代码,就是一个ListBox控件 <Wi ...

  5. 大型项目使用Automake/Autoconf完成编译配置

    http://www.cnblogs.com/xf-linux-arm-java-android/p/3590770.htmlhttp://blog.csdn.net/zengraoli/articl ...

  6. Struts2 请求参数接收

    在Struts2中提供了更为简单的参数请求与接收方法,可以直接在Action中定义属性:Struts2通过反射机制将参数反射到属性的set方法上实现参数的传递: GET方式传送参数 <strut ...

  7. PHP中静态方法和非静态方法的相互调用

    在PHP的非静态方法中可以调用静态方法 class test{ public static function strPrint(){ echo 'this is strPrint static fun ...

  8. Yii的场景

    先上代码 class User extends CActiveRecord{    public function rules()    {        return array(          ...

  9. uva 10306 - e-Coins(完全背包)

    题目链接:10306 - e-Coins 题目大意:给出m和s, 再给出m种电子硬币,每种硬币有两种金额xi,yi.现在要在m种硬币种选若干个硬币,可以重复选同一种硬币, 使得(x1 + x2 + . ...

  10. C++顺序性容器、关联性容器与容器适配器

    什么是容器 首先,我们必须理解一下什么是容器,在C++ 中容器被定义为:在数据存储上,有一种对象类型,它可以持有其它对象或指向其它对像的指针,这种对象类型就叫做容器.很简单,容器就是保存其它对象的对象 ...