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. NOI2013部分题解

    Day 1 T1:向量内积 直接暴力有60.发现将n个向量合成$n\times d$的矩阵$A$,然后求$A\times A^T$,得到的矩阵包含了所有的答案. 先考虑$k=2$,将答案矩阵和全1矩阵 ...

  2. 1.6(java学习笔记)static关键字

    static关键字 1.static修饰变量也称静态变量,静态变量存放在静态区被该类的所有对象共享. 例如,定义了一个类class User{static Sring city = "a城& ...

  3. Scala零基础教学【81-89】

    第81讲:Scala中List的构造是的类型约束逆变.协变.下界详解 首先复习四个概念——协变.逆变.上界.下界 对于一个带类型参数的类型,比如 List[T]: 如果对A及其子类型B,满足 List ...

  4. Asp.Net MVC part2 View、Controller详解

    View详解Razor视图引擎简介HtmlHelper强类型页面 Razor视图引擎简介强大的@:表示使用C#代码,相当于aspx中的<%%>可以完成输出功能当遇到html标签时会认为C# ...

  5. NSNotificationCenter监听TextField文字变化

    注册 1: NSNotificationCenter.defaultCenter().addObserver(self, selector: "textDidChange", na ...

  6. 学习web前端之神器sublime text 3

    第一次在博客园写博客,以前都是看别人写的技术在自己慢慢的学习.现在想自己把每天学习的东西理解并记录下来,加深下印象以后可以做个回顾.不知道自己能否坚持每周至少写2篇博文. 古话说的好:工欲善其事,必先 ...

  7. Mysql -- Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’解决方法

    启动mysql 报错: ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/lib/mysql/m ...

  8. String的解析

    String作为Java中最常用的引用类型,相对来说基本上都比较熟悉,无论在平时的编码过程中还是在笔试面试中,String都很受到青睐,然而,在使用String过程中,又有较多需要注意的细节之处. 1 ...

  9. js调试方法

    参考:1.https://developers.google.com/web/tools/chrome-devtools/javascript/ 2.https://developers.google ...

  10. CocoaPods安装小步骤

    CocoaPod的安装和使用步骤: 1.开启 terminal终端 2.移除现有 Ruby 默认源 $ gem sources --remove https://rubygems.org/ 3.使用新 ...