# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com' 27: Remove Element
https://oj.leetcode.com/problems/remove-element/ Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length. ===Comments by Dabay===
一次循环,两个指针,一个指向插入的位置,另外一个一直往前面走。
如果二号指针的数就是需要删除的数,二号指针继续走。
如果不是要删除的数,把二号指针指向的数移到一号指针的位置上,然后两个指针继续走。
最后跟新数组,返回长度。
''' class Solution:
# @param A a list of integers
# @param elem an integer, value need to be removed
# @return an integer
def removeElement(self, A, elem):
i = j = 0
while j < len(A):
if A[j] != elem:
A[i] = A[j]
i += 1
j += 1
A = A[:i]
return len(A) def main():
sol = Solution()
print sol.removeElement([1,1,2], 1) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[Leetcode][Python]27: Remove Element的更多相关文章

  1. C# 写 LeetCode easy #27 Remove Element

    27. Remove Element Given an array nums and a value val, remove all instances of that value in-place  ...

  2. 【LeetCode】27. Remove Element (2 solutions)

    Remove Element Given an array and a value, remove all instances of that value in place and return th ...

  3. 【LeetCode】27. Remove Element 解题报告(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 记录起始位置 日期 题目地址:https:/ ...

  4. 【LeetCode】27 - Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  5. 【一天一道LeetCode】#27. Remove Element

    一天一道LeetCode系列 (一)题目 Given an array and a value, remove all instances of that value in place and ret ...

  6. Leetcode No.27 Remove Element(c++实现)

    1. 题目 1.1 英文题目 Given an integer array nums and an integer val, remove all occurrences of val in nums ...

  7. LeetCode OJ 27. Remove Element

    Given an array and a value, remove all instances of that value in place and return the new length. D ...

  8. LeetCode:27. Remove Element(Easy)

    1. 原题链接 https://leetcode.com/problems/remove-element/description/ 2. 题目要求 给定一个整数数组 nums[ ] 和一个整数 val ...

  9. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

随机推荐

  1. MySQL必知必会笔记<2>

    [英]ben Forta著 5 1.0  *使用扩展查询* |---->select note from table   where Match(note) Against('anl'); |- ...

  2. Service(Local Service)简介

    一.Service的作用 主要用于在后台处理一些耗时的逻辑操作,或者去执行某些长期运行的操作. 二.Service的创建 1.主要方法 IBinder onBind(Intent intent):必须 ...

  3. .Net缓存

    近期研究了一下.Net的缓存,据说可以提高系统的性能. .Net缓存分为两种HttpRuntime.Cache和HttpContext.Current.Cache 不过从网上查找资料,说两种缓存其实是 ...

  4. poj1004

    Financial Management Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 125635   Accepted: ...

  5. SEO人员应该突破的5大思想误区

    1.外链误区 很多人在做网站优化的时候容易陷入外链怪圈,认为外链就是网站优化的一切,只要做好外链排名就会上去,让他不做外链了他将不知道做什么.特别是外链专员和一些初级SEO人员,优化网站只知道到哪里发 ...

  6. Java WebService把Date类型转换成XMLGregorianCalendar

    JavaEE 的WebService中的Date类型在Web应用中调set方法的时候,默认情况下,JAXB将xsd:date, xsd:time, 和xsd:dateTime映射为XMLGregori ...

  7. Android中API建议的方式实现SQLite数据库的增、删、改、查的操作

    package com.examp.use_SQLite.dao; import java.util.ArrayList; import java.util.List; import android. ...

  8. 【每天一个Linux命令】14. Linux中locate命令的用法

    命令用途 locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案. 其方法是先建立一个包括系统内所有档案名称及路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中了. ...

  9. ORA-02287: 此处不同意序号

    ORA-02287: 此处不同意序号 insert into gls_vchitem     (viid,      yr,      km)     select gls_vchitem_seq.n ...

  10. android 网络状态判断【转】

    import java.net.InetAddress; import android.app.Activity;import android.content.Context;import andro ...