[Leetcode][Python]26: Remove Duplicates from Sorted Array
# -*- 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的更多相关文章
- 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 ...
- 【leetcode】 26. Remove Duplicates from Sorted Array
@requires_authorization @author johnsondu @create_time 2015.7.22 18:58 @url [remove dublicates from ...
- 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...
- 【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 ...
- 【一天一道LeetCode】#26. Remove Duplicates from Sorted Array
一天一道LeetCode系列 (一)题目 Given a sorted array, remove the duplicates in place such that each element app ...
- 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 ...
- 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- ...
- LeetCode:26. Remove Duplicates from Sorted Array(Easy)
1. 原题链接 https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/ 2. 题目要求 给定一个已 ...
- 【一天一道LeetCode】#80. Remove Duplicates from Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
随机推荐
- Scala基础入门-代码碎片
import scala.util.control._ import java.util.Date object Test { def main(args: Array[String]) { // v ...
- php中__autoload()方法详解
[导读] PHP在魔术函数__autoload()方法出现以前,如果你要在一个程序文件中实例化100个对象,那么你必须用include或者require包含进来100个类文件,或者你把这100个类定义 ...
- linux下小记
今天碰到一个问题 记录下 /usr/bin/ld: cannot find ld 和ldconfig的区别 使用makefile编译的时候提示ld提示某个so找不到 当时使用ldconfig查了下 发 ...
- logstash 操作redis
在实际应用中,Logstash进程会被氛围两个不同的角色. 运行在应用服务器上的尽量减轻运行压力,只做读取和转发,这个角色叫做shipper 运行在独立的服务器上完成数据解析处理,负责写入到Elast ...
- XMPP iOS开发(IM开发,转)
搭建完本地服务器之后,我们便可以着手客户端的工作,这里我们使用XMPPFramework这个开源库,安卓平台可以使用Smack(最好使用4.1以及之后的版本,支持流管理),为了简单起见这里只实现登陆. ...
- 传纸条(一)(双线程dp)
传纸条(一) 时间限制:2000 ms | 内存限制:65535 KB 难度:5 描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行 ...
- google自定义站内搜索
ttps://www.google.com/cse/docs/cref.html?hl=zh-cn 重要表单参数: action 字段:您希望存储结果的网址(在该例中,我们使用 http://www. ...
- linux shadow破解
主要学习了下linux /etc/shadow文件中密码的破解:学习了hashcat工具的简单实用,具体的可以参加:https://samsclass.info/123/proj10/p12-hash ...
- 兼容 CommonJS 和 CommonJS-like规范(1~38)
CommonJS是服务器端模块的规范,Node.js采用了这个规范. 根据CommonJS规范,一个单独的文件就是一个模块.每一个模块都是一个单独的作用域,也就是说,在该模块内部定义的变量,无法被其他 ...
- VS中,无法嵌入互操作类型“……”,请改用适用的接口的解决方法
最近使用VS,在引用COM组件的时候,出现了无法嵌入互操作类型“……”,请改用适用的接口的错误提示.查阅资料,找到解决方案,记录如下: 选中项目中引入的dll,鼠标右键,选择属性,把“嵌入互操作类型” ...