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. 【LCA倍增】POJ1330-Nearest Common Ancestors

    [知识点:离线算法&在线算法] 一个离线算法,在开始时就需要知道问题的所有输入数据,而且在解决一个问题后就要立即输出结果. 一个在线算法是指它可以以序列化的方式一个个的处理输入,也就是说在开始 ...

  2. python3开发进阶-Web框架的前奏

    我们可以这样理解:所有的Web应用本质上就是一个socket服务端,而用户的浏览器就是一个socket客户端. 这样我们就可以自己实现Web框架了. 1.自定义web框架 import socket ...

  3. python基础-协程函数、递归、模块、包等内容

    1. 协程函数 1.1 yield基本用法 yield作用 1.把函数的执行结果封装好,即封装__iter__和__next__,即得到一个迭代器 2.与return功能类似,都可以返回值,但不同的是 ...

  4. HashMap源码-使用说明部分

    /* * Implementation notes. * 使用说明 * * This map usually acts as a binned (bucketed) hash table, but * ...

  5. Delphi Delay 延时计数的功能。 下面的方法都是思路,但是没有用在项目上

    procedure Tfrm_InstrumentControl.aa;var CurLength: Word; vTimeLength: Word;begin Screen.Cursor := cr ...

  6. 最短路径之迪杰斯特拉算法的Java实现

    Dijkstra算法是最短路径算法中为人熟知的一种,是单起点全路径算法.该算法被称为是“贪心算法”的成功典范.本文接下来将尝试以最通俗的语言来介绍这个伟大的算法,并赋予java实现代码. 一.知识准备 ...

  7. 【java】乱码处理+编码转化+判断字符串编码方式

    之前有一篇是修改IDE的编码,服务器的编码等处理乱码,但是在所有环境因素上,保证了编码方式之后,也会有前台传递给后台[get方式提交]传递给后台的编码方式是非UTF-8的,也会有例如FTP服务器的编码 ...

  8. shell产生随机数七种方法

    shell实例浅谈之三产生随机数七种方法   一.问题 Shell下有时需要使用随机数,在此总结产生随机数的方法.计算机产生的的只是“伪随机数”,不会产生绝对的随机数(是一种理想随机数).伪随机数在大 ...

  9. Thinkphp2.1爆出重大安全漏洞

    thinkphp 2.1的版本 我们来分析下漏洞吧 官方发布了一个安全补丁 表述是:该URL安全漏洞会造成用户在客户端伪造URL,执行非法代码. 官方的补丁: /trunk/ThinkPHP/Lib/ ...

  10. LWIP的移植笔记

    第一次发表博客,文章摘录于还不懂同学的专栏 lwIp的作者做了大量的工作以方便像我这种懒人移植该协议栈,基本上只需修改一个配置头文件和改写3个函数即可完成lwIP的移植.要改写的函数位于lwIP-1. ...