# Q27 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 by modifying the input array in-place with O(1) extra memory.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

##### Example:
```Given nums = [3,2,2,3], val = 3,

Your function should return length = 2, with the first two elements of nums being 2.```

##### Pyhton实现：
```class Solution:
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
i = 0
while i < len(nums):
if nums[i] is val:
del nums[i]
else:
i +=1
return len(nums)

a = [3,2,2,3]
b = Solution()
print(b.removeElement(a,3))  # 2```

0 条评论

• ### Q26 Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in-place such that each element appe...

• ### Q189 Rotate Array

Rotate an array of n elements to the right by k steps. For example, with n = 7 a...

• ### Q503 Next Greater Element II

Given a circular array (the next element of the last element is the first elemen...

• ### LeetCode第27题

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

• ### Array - 80. Remove Duplicates from Sorted Array II

80、Remove Duplicates from Sorted Array II 相似题型： 26

• ### 数据结构算法操作试题(C++/Python)——下一个排列

leetcode 链接：https://leetcode-cn.com/problems/next-permutation/

• ### Q26 Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in-place such that each element appe...

• ### 三数之和姊妹题——LeetCode题目16：最接近的三数之和

给定一个包括 n 个整数的数组 nums 和 一个目标值 target。找出 nums 中的三个整数，使得它们的和与 target 最接近。返回这三个数的和。假...

• ### 汇总区间

返回 恰好覆盖数组中所有数字 的 最小有序 区间范围列表。也就是说，nums 的每个元素都恰好被某个区间范围所覆盖，并且不存在属于某个范围但不属于 nums 的...

• ### 【Leet Code】26. Remove Duplicates from Sorted Array

版权声明：本文为博主原创文章，遵循 CC 4.0 BY-SA 版权协议，转载请附上原文出处链接和本声明。