• [Leetcode][Python]27: Remove Element


    # -*- 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)
  • 相关阅读:
    NOIP2016 蚯蚓 题解
    BZOJ 1294 围豆豆 题解
    POJ1852 Ants 题解
    BZOJ 1131 [POI2008] STA-Station 题解
    HDU 5963 朋友 题解
    Codeforces 1292C Xenon's Attack on the Gangs 题解
    Emergency Evacuation 题解
    P4408 逃学的小孩 题解
    UVA11300 Spreading the Wealth 题解
    P2882 Face The Right Way G 题解
  • 原文地址:https://www.cnblogs.com/Dabay/p/4261424.html
Copyright © 2020-2023  润新知