LeetCode_27. Remove Element
27. Remove Element
Given an array nums and a value val, 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 by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
Example 1:
Given nums = [3,2,2,3], val = 3, Your function should return length = 2, with the first two elements of nums being 2. It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = [0,1,2,2,3,0,4,2], val = 2, Your function should return length =5
, with the first five elements ofnums
containing0
,1
,3
,0
, and 4. Note that the order of those five elements can be arbitrary. It doesn't matter what values are set beyond the returned length.
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
// nums is passed in by reference. (i.e., without making a copy)
int len = removeElement(nums, val); // any modification to nums in your function would be known by the caller.
// using the length returned by your function, it prints the first len elements.
for (int i = 0; i < len; i++) {
print(nums[i]);
}
package leetcode.easy; public class RemoveElement {
@org.junit.Test
public void test() {
int[] nums11 = { 3, 2, 2, 3 };
int[] nums12 = { 0, 1, 2, 2, 3, 0, 4, 2 };
int[] nums21 = { 3, 2, 2, 3 };
int[] nums22 = { 0, 1, 2, 2, 3, 0, 4, 2 };
int val1 = 3;
int val2 = 2;
System.out.println(removeElement1(nums11, val1));
System.out.println(removeElement1(nums12, val2));
System.out.println(removeElement2(nums21, val1));
System.out.println(removeElement2(nums22, val2));
} public int removeElement1(int[] nums, int val) {
int i = 0;
for (int j = 0; j < nums.length; j++) {
if (nums[j] != val) {
nums[i] = nums[j];
i++;
}
}
return i;
} public int removeElement2(int[] nums, int val) {
int i = 0;
int n = nums.length;
while (i < n) {
if (nums[i] == val) {
nums[i] = nums[n - 1];
// reduce array size by one
n--;
} else {
i++;
}
}
return n;
}
}
LeetCode_27. Remove Element的更多相关文章
- 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element
Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...
- [LeetCode] Remove Element 分析
Remove Element算是LeetCode的一道水题,不过这题也有多种做法,现就我所知的几种做一点讨论. 题目链接:https://leetcode.com/problems/remove-el ...
- Remove Element,Remove Duplicates from Sorted Array,Remove Duplicates from Sorted Array II
以下三个问题的典型的两个指针处理数组的问题,一个指针用于遍历,一个指针用于指向当前处理到位置 一:Remove Element Given an array and a value, remove a ...
- [Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- [array] leetCode-27. Remove Element - Easy
27. Remove Element - Easy descrition Given an array and a value, remove all instances of that value ...
- leetcode-algorithms-27 Remove Element
leetcode-algorithms-27 Remove Element Given an array nums and a value val, remove all instances of t ...
- 【LeetCode】27. Remove Element (2 solutions)
Remove Element Given an array and a value, remove all instances of that value in place and return th ...
- [LeetCode] Remove Element题解
Remove Element: Given an array and a value, remove all instances of that value in place and return t ...
随机推荐
- git如何利用分支进行多人开发
在使用git时,假如远程仓库有 dev 和 master 两个分支,master 作为一个稳定版分支,可用于直接发布产品,日常的开发则 push 到 dev 分支,那本地是不是要从 dev 分支中创建 ...
- redis 事务 & 锁
参考:https://www.cnblogs.com/DeepInThought/p/10720132.html Redis不保证原子性:Redis中,单条命令是原子性执行的,但事务不保证原子性,且没 ...
- [NOI2013]快餐店 / CF835F Roads in the Kingdom (基环树)
题意 一颗基环树,选一对点使得这两个点的最短距离最大. 题解 相当于找基环树的直径,但是这个直径不是最长链,是基环树上的最短距离. 然后不会做. 然后看了ljh_2000的博客. 然后会了. 这道题最 ...
- Ubuntu 16.04安装Docker-Compose 与 Can't connect to docker from docker-compose
根据别的 网址做一个记录 : https://www.linuxidc.com/Linux/2017-01/139574.htm Linux环境 Ubuntu 16.04(LTS)curl安装安装 ...
- sql server 变量和select 赋值的联合使用demo
) ) select @cltcode=cltcode,@brand=brand from prosamplehd CREATE table #t ( cltcode ), brand ) ) INS ...
- MongoDB 了解正在进行的操作
1.1 查看正在进行的操作 使用db.currentOp()函数: >db.currentOp() 1.opid 这是操作的唯一标识符,可以通过它来终止操作 2.active 表示操作是否正在进 ...
- Windows10+Jupyter notebook+添加核
链接:https://blog.csdn.net/ZWX2445205419/article/details/80113472 1. 安装Anaconda 2. 创建虚拟环境 > con ...
- mac 安装 mysql 5.7
下载 https://dev.mysql.com/downloads/mysql/5.7.html#downloads 下一步,经过一系列安装步骤后,会跳出一个这样的界面,请注意!!! 上面红框中是你 ...
- Linux下 为什么有时候使用sudo也提示没有权限
例如: #sudo echo 1 > /proc/sys/net/ipv6/conf/all/disable_ipv6 bash: /proc/sys/net/ipv6/conf/all/dis ...
- (转)hadoop 常规错误问题(一)
转至:http://www.freeoa.net/osuport/db/my-hbase-usage-problem-sets_2979.html 本文是我在使用Hbase的过程碰到的一些问题和相应的 ...