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.

最新文章

  1. C#反射机制 (转载)
  2. HTML5中判断横屏竖屏
  3. NoSQL 精粹
  4. file
  5. sqlserver 中的NOLOCK、HOLDLOCK、UPDLOCK、TABLOCK、TABLOCKX
  6. tar+gzip
  7. 做 fzu oj 1106 题目学到的
  8. 阿里巴巴SUI Mobile的使用
  9. App Store生存指南
  10. PHP获取生成一个页面的数据库查询次数(转)
  11. 去英国Savile Row 做件私人定制手工西装_GQ男士网
  12. 堡垒机--paramiko模块
  13. 利用宏定义实现C++程序在Unix和Win32环境下的通用性
  14. 02-从零玩转JavaWeb-类与对象
  15. WPF 简易的喷泉效果
  16. 学习日记之工厂方法模式和Effective C++
  17. C++继承分析
  18. 5.创建表,使用alter进行表信息的增删改,Oracle回收站,集合运算
  19. web services + soap + wsdl 学习
  20. 如何配置Bitbucket的ssh

热门文章

  1. 网络编程-tcp
  2. Ubuntu 16.04下使用UNetbootin制作的ISO镜像为U盘启动出现:Missing Operating System (mbr.bin)
  3. Javascript高级程序设计-问答模式
  4. hdu 1863 畅通project
  5. pandas判断缺失值的办法
  6. 理解JS里的偏函数与柯里化
  7. 转:解决 java.util.MissingResourceException: Can&#39;t find bundle for base name com...config, locale zh_CN 错误
  8. 【HTTPS双向加密认证】
  9. testform
  10. vue - rimraf