LeetCode#27 | Remove Element 移除元素

一、题目

Given an array and a value, remove all instances of that value in-place and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn‘t matter what you leave beyond the new length.

Example 1:
Given nums = [3,2,2,3], val = 3,
Your function should return length = 2, with the first two elements of nums being 2.
It doesn‘t matter what you leave beyond the returned length.

Example 2:
Given nums = [0,1,2,2,3,0,4,2], val = 2,
Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.
Note that the order of those five elements can be arbitrary.
It doesn‘t matter what values are set beyond the returned length.

移除元素
给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度。
不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。
元素的顺序可以改变。不需要考虑数组中超出新长度后面的元素。

二、题解

  • 解法1:暴力法
    遍历数组,找到与搜索值相同的元素就删除,则遍历到尾后,剩下的数组就是去重的数组。
    时间复杂度是O(n)。
function removeElement(&$nums, $val) {
    foreach ($nums as $k => $v) {
        if ($v == $val) {
            unset($nums[$k]);
        }
    }
    return count($nums);
}
  • 解法2:双指针法
    删除排序数组中的重复项的解法相似,设置快慢指针。i是慢指针,j是遍历数组时的快指针。
    当 nums[j] 与给定的值相等时,递增 j 以跳过该元素。只要 nums[j] ≠ val,我们就复制 nums[j] 到 nums[i] 并同时递增两个索引。重复这一过程,直到 j 到达数组的末尾,则该数组的新长度为 i。
    时间复杂度:O(n),空间复杂度:O(1)。
function removeElement(&$nums, $val) {
    $i = 0;
    for ($j = 0; $j < count($nums); $j++) {
        if ($nums[$j] != $val) {
            $nums[$i] = $nums[$j];
            $i++;
        }
    }
    return $i;
}

原文地址:https://www.cnblogs.com/sunshineliulu/p/12364707.html

时间: 2024-07-30 23:06:31

LeetCode#27 | Remove Element 移除元素的相关文章

[LeetCode]13. 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. 解法1:先遍历数组统计出数组中含有多少个指定元素,然后遍历数组,从头开始找第一个出现指定值的位置,从尾开始找第一个

LeetCode 27.Remove Element 数组元素删除

27. Remove Element Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be changed.

leetCode 27.Remove Element (删除元素) 解题思路和方法

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. 思路:此题和26题一脉相承,算法上不难,具体如代码所示: public class

LeetCode 27 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 lengt

[LeetCode] 27. Remove Element ☆

Given an array and a value, remove all instances of that value in place and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. The order of elements can be changed. It doesn't matter

LeetCode 27 Remove Element (C,C++,Java,Python)

Problem: 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. Solution: 和26题一样,就是判断条件不一样而已. 题目大意: 给一个数组,要求返回删除

leetcode 27 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. 题解:水题.遇到val就和最后一个不是val的数交换位置就好了. class Solution { public:

Leetcode 27 Remove Element STL

和remove zero类似的方法完成该题 1 class Solution { 2 public: 3 int removeElement(vector<int>& nums, int val) { 4 vector<int>::size_type j = 0; 5 for(vector<int>::size_type i = 0; i < nums.size(); ++i){ 6 if(nums[i] != val) nums[j++] = nums[

No.27 Remove Element

No.27 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. Tags: Array Two Pointers 移除数组中所有的给定数