# -*- 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. zend guard loader

    1 .是zendoptimizer的前身, 在php 5.3 (含)之前使用更新到6 ,5.4 之后不再使用.是代码优化的一种,7中opcache 类似功效. 2 .php版本的变量 phpversi ...

  2. LGA(land grid array)

    产品应用 1.射频功放 2.加速度传感器 3.地磁传感器 可靠性 Reliability 高压蒸煮 PCT 121℃,100%RH,2atm,96hrs 高低温循环 TCT -55℃(15min)~1 ...

  3. LPC同STM32的比较

    Cortex-M3是新兴起来的一种ARM7的核,而ARM7TDMI是一种传统的经典的ARM内核.我们就抛开这一切,来比较一下两则的异同. 我们就在以下平台上比较吧: STMicoelectronics ...

  4. why constrained regression and Regularized regression equivalent

    problem 1: $\min_{\beta} ~f_\alpha(\beta):=\frac{1}{2}\Vert y-X\beta\Vert^2 +\alpha\Vert \beta\Vert$ ...

  5. LFM 隐语义模型

    隐语义模型: 物品       表示为长度为k的向量q(每个分量都表示  物品具有某个特征的程度) 用户兴趣 表示为长度为k的向量p(每个分量都表示  用户对某个特征的喜好程度) 用户u对物品i的兴趣 ...

  6. Qt开发初步,循序渐进,preRequest for 蓝图逆袭

    1,使用Qt面向对象类继承创建第一个窗口主部件,使用setMinimumSize(),setMaximumSize()配置主部件窗口是否能够resize;

  7. poj 3661 Running(区间dp)

    Description The cows are trying to become better athletes, so Bessie ≤ N ≤ ,) minutes. During each m ...

  8. 动画原理——脉动(膨胀缩小)&&无规则运动

    书籍名称:HTML5-Animation-with-JavaScript 书籍源码:https://github.com/lamberta/html5-animation 1.脉动是一种半径r来回反复 ...

  9. 通过自定义注解反射生成SQL语句

    ----------------------------------------Program.cs---------------------------------------- using Sys ...

  10. Spring 整合hibernante 错误java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener

    1.将所需的jar包全部拷贝到WEB-INF/lib下,再重新启动tomcat便能顺利通过了.参考http://blessht.iteye.com/blog/1104450 最佳答案   spring ...