27. Remove Element【easy】

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 what you leave beyond the new length.

Example:
Given input array nums = [3,2,2,3]val = 3

Your function should return length = 2, with the first two elements of nums being 2.

解法一:

 class Solution {
public:
int removeElement(vector<int>& nums, int val) {
if (nums.empty()) {
return ;
} int i = ;
int j = ;
while (i < nums.size()) {
if (nums[i] != val) {
nums[j++] = nums[i++];
}
else {
++i;
}
} return j;
}
};

双指针

解法二:

 public int removeElement(int[] nums, int val) {
int i = ;
for (int j = ; j < nums.length; j++) {
if (nums[j] != val) {
nums[i] = nums[j];
i++;
}
}
return i;
}

Intuition

Since question asked us to remove all elements of the given value in-place, we have to handle it with O(1) extra space.

How to solve it? We can keep two pointers i and j, where i is the slow-runner while j is the fast-runner.

Algorithm

When nums[j] equals to the given value, skip this element by incrementing j. As long as nums[j]≠val, we copy nums[j] to nums[i] and increment both indexes at the same time.

Repeat the process until j reaches the end of the array and the new length is i.

解法三:

 public int removeElement(int[] nums, int val) {
int i = ;
int n = nums.length;
while (i < n) {
if (nums[i] == val) {
nums[i] = nums[n - ];
// reduce array size by one
n--;
} else {
i++;
}
}
return n;
}

Intuition

Now consider cases where the array contains few elements to remove. For example, nums = [1,2,3,5,4], val = 4.

The previous algorithm will do unnecessary copy operation of the first four elements. Another example is nums = [4,1,2,3,5], val = 4.

It seems unnecessary to move elements [1,2,3,5]one step left as the problem description mentions that the order of elements could be changed.

Algorithm

When we encounter nums[i] = val, we can swap the current element out with the last element and dispose the last one. This essentially reduces the array's size by 1.

Note that the last element that was swapped in could be the value you want to remove itself. But don't worry, in the next iteration we will still check this element.

27. Remove Element【easy】的更多相关文章

  1. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  2. LeetCode:27. Remove Element(Easy)

    1. 原题链接 https://leetcode.com/problems/remove-element/description/ 2. 题目要求 给定一个整数数组 nums[ ] 和一个整数 val ...

  3. 83. Remove Duplicates from Sorted List【easy】

    83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...

  4. 26. Remove Duplicates from Sorted Array【easy】

    26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...

  5. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  6. 121. Best Time to Buy and Sell Stock【easy】

    121. Best Time to Buy and Sell Stock[easy] Say you have an array for which the ith element is the pr ...

  7. 448. Find All Numbers Disappeared in an Array【easy】

    448. Find All Numbers Disappeared in an Array[easy] Given an array of integers where 1 ≤ a[i] ≤ n (n ...

  8. 1. Two Sum【easy】

    1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...

  9. 167. Two Sum II - Input array is sorted【easy】

    167. Two Sum II - Input array is sorted[easy] Given an array of integers that is already sorted in a ...

随机推荐

  1. 【线段树】bzoj3922 Karin的弹幕

    设置一个值K. d<=K:建立多组线段树:d>K:暴力. 最优时间复杂度的伪计算: O(n*K*logn(建树)+m*logn(询问类型1)+m*n/K(询问类型2)+m*K*logn(修 ...

  2. 【分块】【常数优化】【Orz faebdc】洛谷 P1083 NOIP2012提高组 借教室

    分块90分. By AutSky_JadeK [重点在下面] #include<cstdio> #include<cmath> using namespace std; #de ...

  3. 【匈牙利算法模板】BZOJ1191-超级英雄

    #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...

  4. [转]从此爱上iOS Autolayout

    原文地址 这篇不是autolayout教程,只是autolayout动员文章和经验之谈,在本文第五节友情链接和推荐中,我将附上足够大家熟练使用autolayout的教程.这篇文章两个月前就想写下来,但 ...

  5. Word绘制跨行表格

    如图“用户评价电影数目”,我们需要均分第一行,选中这三个个,设置了表个高度0.5cm,但是发现上面的一个比较考上,我们需要找到水平竖直居中,那么双击表格,打开表格工具,有设计和布局,切换到布局就找到了 ...

  6. 精通JavaScript攻击框架:AttackAPI

    多年来客户端安全一直未引起人们的足够重视,但是如今情况发生了急剧转变,客户端安全已经成为信息安全领域的焦点之一.Web恶意软件.AJAX蠕虫.浏览历史暴破.登录检测.傀儡控制技术网络端口扫描以及浏览器 ...

  7. uCOS-ii笔记

    ucos ii system 文件结构 上层: 应用软件,用户代码 中层: 与处理器无关代码 与应用程序相关配置文件 与处理器有关代码 下层: 硬件(cpu,interupt,timer,gpio,i ...

  8. 【ztree系列——图标的修改】Bootstrap风格的ztree

    前段时间项目中需要用树形结构,在选取的时候参考了很多插件,经过很多尝试,最后又回归到了ztree上.以前用的界面框架是EasyUI,但是它的树结构在实现起来有点复杂,并且功能不是特别完善.dtree在 ...

  9. Java LinkedList的模拟实现

    双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点.查询即从第一个节点,不断指 ...

  10. CocoSourcesCS 4

    /*------------------------------------------------------------------------- ParserGen.cs -- Generati ...