Skip to content

Commit 6a9c616

Browse files
committed
27 remove elements
1 parent 068f26d commit 6a9c616

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

leet code/27.remove_elements.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# 27. Remove Element
2+
# Given an integer array nums and an integer val, remove all occurrences of val in nums in-place.
3+
# The order of the elements may be changed.
4+
# Then return the number of elements in nums which are not equal to val.
5+
6+
# Consider the number of elements in nums which are not equal to val be k, to get accepted,
7+
# you need to do the following things:
8+
9+
# Change the array nums such that the first k elements of nums contain the elements which are not equal to val.
10+
# The remaining elements of nums are not important as well as the size of nums.
11+
# Return k.
12+
13+
# Example 1:
14+
# Input: nums = [3,2,2,3], val = 3
15+
# Output: 2, nums = [2,2,_,_]
16+
# Explanation: Your function should return k = 2, with the first two elements of nums being 2.
17+
# It does not matter what you leave beyond the returned k (hence they are underscores).
18+
19+
def removeElement(nums, val):
20+
while val in nums:
21+
i=nums.index(val)
22+
nums.pop(i)
23+
return len(nums)
24+

0 commit comments

Comments
 (0)