TreeviewCopyright © aleen42 all right reserved, powered by aleen42
27. 移除元素
https://leetcode-cn.com/problems/remove-element/
Java
class Solution {
public int removeElement(int[] nums, int val) {
int count = 0;
for(int i=0;i<nums.length;i++) {
if(nums[i]!=val) {
nums[count] = nums[i];
count++;
}
}
return count;
}
}
Python
'''
@Author: Goog Tech
@Date: 2020-07-22 21:58:29
@Description: https://leetcode-cn.com/problems/remove-element/
@FilePath: \leetcode-googtech\#27. Remove Element\Solution.py
'''
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
while val in nums:
nums.remove(val)
return len(nums)