[Leetcode][Python]27: Remove Element

# -*- coding: utf8 -*-‘‘‘__author__ = ‘[email protected]‘

27: Remove Elementhttps://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)
时间: 2024-10-29 19:08:32

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

【LeetCode】27 - 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. Solution 1: 设置一个新的下标length,遍历过程中遇到val就跳过,否则就赋给新数组下标对应元素.缺

LeetCode OJ 27. Remove Element

Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be changed. It doesn't matter

[LeetCode] NO. 27 Remove Element

[题目] Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be changed. It doesn't ma

【LeetCode】27. Remove Element 解题小结

题目:Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be changed. It doesn't matt

LeetCode:27. Remove Element(Easy)

1. 原题链接 https://leetcode.com/problems/remove-element/description/ 2. 题目要求 给定一个整数数组 nums[ ] 和一个整数 val,删除数组中与val相同的元素,并返回删除后的数组长度 注意:不能定义新的数组,只能使用O(1)空间大小 3. 解题思路 遍历一次,将每个元素与给定的value进行比较,不同则给nums[count++]赋予当前元素的值:相同则直接跳过,最后返回count,即为删除后数组的长度. 4. 代码实现 p

LeetCode 27.Remove Element 数组元素删除

27. Remove Element Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be changed.

27. Remove Element【easy】

27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be ch

Leetcode 线性表 Remove Element

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Remove Element Total Accepted: 13840 Total Submissions: 42676 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 change

No.27 Remove Element

No.27 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. Tags: Array Two Pointers 移除数组中所有的给定数