-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path27_RemoveElement.py
More file actions
43 lines (34 loc) · 940 Bytes
/
27_RemoveElement.py
File metadata and controls
43 lines (34 loc) · 940 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
from typing import List
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
if len(nums) == 0:
return 0
i = 0
for num in nums:
if num != val:
nums[i] = num
i += 1
return i
def main():
# Example:1
nums = [3, 2, 2, 3]
val = 3
print(Solution().removeElement(nums, val))
# Input: nums = [3,2,2,3], val = 3
# Output: 2, nums = [2,2,_,_]
# Example:2
nums = [0, 1, 2, 2, 3, 0, 4, 2]
val = 2
print(Solution().removeElement(nums, val))
# Input:nums = [0,1,2,2,3,0,4,2], val = 2
# Output: 5, nums = [0, 1, 4, 0, 3, _, _, _]
# Example:3
# list1 = [], list2 = []
# print(Solution().mergeTwoLists(list1, list2))
# Input: list1 = [], list2 = [0]
# Output: [0]
# Error case
# print(Solution().isValid("]"))
# # Output: false
if __name__ == '__main__':
main()