TreeviewCopyright © aleen42 all right reserved, powered by aleen42

27. 移除元素

https://leetcode-cn.com/problems/remove-element/

Java

/*
 * @Author: Goog Tech
 * @Date: 2020-07-22 21:58:21
 * @Description: https://leetcode-cn.com/problems/remove-element/
 * @FilePath: \leetcode-googtech\#27. Remove Element\Solution.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)
Copyright © GoogTech 2021 all right reserved,powered by GitbookLast update time : 2021-09-15 01:55:05

results matching ""

    No results matching ""