27. Remove Element【easy】
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】的更多相关文章
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- LeetCode:27. Remove Element(Easy)
1. 原题链接 https://leetcode.com/problems/remove-element/description/ 2. 题目要求 给定一个整数数组 nums[ ] 和一个整数 val ...
- 83. Remove Duplicates from Sorted List【easy】
83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...
- 26. Remove Duplicates from Sorted Array【easy】
26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 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 ...
- 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 ...
- 1. Two Sum【easy】
1. Two Sum[easy] Given an array of integers, return indices of the two numbers such that they add up ...
- 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 ...
随机推荐
- [CF919E]Congruence Equation
题意:求关于$n$的方程$n\cdot a^n\equiv b\left(mod\ p\right)$在$[1,x]$中整数解的数量 果然是Chinese round,interesting roun ...
- Java概述--Java开发实战经典
1)Java有三个发展方向,分别是Java SE,Java EE,Java ME.以下简要介绍. a.Java SE,Java Standard Edition(java标准版),包含了构成java语 ...
- Spring入门程序-前端控制器配置器
1,处理器的第二种配置方式 <!--配置handler --> <bean id="/FirstController" class="com.songy ...
- RxJava 1.x 理解-2
给RxJava 加入线程控制 -- Scheduler 在 RxJava 1.x 理解-1 中,我们说到了RxJava的简单用法,但是这还远远不够,因为这简单用法是在同一个线程中使用的.比如我们需要在 ...
- [Bug]转:使用jquery的 uploadify,在谷歌浏览器上总会崩溃的解决方法
最近做的项目使用了jquery的uploadify,但是在谷歌浏览器测试总是会出现崩溃.如: 因为是java项目. 解决的办法是: 给引入的js加上一个参数,时间戳就可以,防止缓存,使每一次都请求.( ...
- Telnet环境变量
转:http://www.cnpaf.net/Class/Telnet/200408/2.html 当前位置: 网站首页>>协议大全>>TELNET协议>> Tel ...
- 关于group by后为每个分组编号
- pl/sql游标
通过游标,我们可以取得返回结果集的任何一行记录,提高效率. 定义游标: type 游标名 is ref cursor 变量名 游标名 打开游标: open 游标变量 for select语句: 取出当 ...
- druid.io使用技术简介: Hyperloglog
druid.io 使用Hyperloglog 估计基数 参照如下连接 http://blog.codinglabs.org/articles/algorithms-for-cardinality-es ...
- junit4单元測试总结
junit4单元測试总结 本文开发环境为myeclipse10.7 1. 准备工作 1.1. 选择须要单元測试的文件 创建mavenproject.右击须要单元測试的文件,选择New->oth ...