当前位置: 首页 > 文档资料 > 算法珠玑 >

linear-list/array/remove-element

优质
小牛编辑
129浏览
2023-12-01

Remove Element

描述

Given an array and a value, remove all instances of that value in place and return the new length.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

分析

代码

# Remove Element
# 双指针
# Time Complexity: O(n), Space Complexity: O(1)
class Solution:
    def removeElement(self, nums: List[int], target: int) -> int:
        i = 0
        for j in range(len(nums)):
            if nums[j] != target:
                nums[i] = nums[j]
                i += 1
        return i
// Remove Element
// 双指针
// Time Complexity: O(n), Space Complexity: O(1)
public class Solution {
    public int removeElement(int[] nums, int target) {
        int i = 0;
        for (int j = 0; j < nums.length; ++j) {
            if (nums[j] != target) {
                nums[i++] = nums[j];
            }
        }
        return i;
    }
};
// Remove Element
// 双指针
// Time Complexity: O(n), Space Complexity: O(1)
class Solution {
public:
    int removeElement(vector<int>& nums, int target) {
        int i = 0;
        for (int j = 0; j < nums.size(); ++j) {
            if (nums[j] != target) {
                nums[i++] = nums[j];
            }
        }
        return i;
    }
};

相关题目