Skip to content

Commit 2da8080

Browse files
committed
problem solving
1 parent d143418 commit 2da8080

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

python-problem-solving/removeNo.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
3+
4+
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
5+
6+
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
7+
8+
Example 1:
9+
10+
Given nums = [3,2,2,3], val = 3,
11+
12+
Your function should return length = 2, with the first two elements of nums being 2.
13+
14+
It doesn't matter what you leave beyond the returned length.
15+
16+
'''
17+
18+
19+
class Solution(object):
20+
def removeElement(self, nums, val):
21+
"""
22+
:type nums: List[int]
23+
:type val: int
24+
:rtype: int
25+
"""
26+
j = 0
27+
for n in nums:
28+
if n != val:
29+
nums[j] = n
30+
j += 1
31+
return j

0 commit comments

Comments
 (0)