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 ...
随机推荐
- DEVC++如何调试代码
DEVC++小技巧 学习C语言的同学大多都会使用DEVC++这个软件,但是在使用的时候会发现是不可以调试的,因为我们的软件默认是将调试关闭了的.下面是调试的具体方法. 点击窗口的工具按钮 点击编辑按钮 ...
- 配置Cisco网络设备
了解就行,不用记 电脑管理路由器软件 路由器显示命令: router#show run :显示配置信息 router#show interface :显示接口信息 router#show ip r ...
- Pthon操作Gitlab API----批量删除,创建,取消保护
1.需求:大批量的应用上线后合并到Master,其他的分支develop/test/uat等需要同步最新代码的操作. 2.操作:可以通过传参 ,列表 的方式把每个项目的id值填入,才能对相关项目进行批 ...
- Java中String、StringBuilder和StringBuffer
StringBuilder和StringBuffer内部都是通过char[]来实现的.(jdk1.9后,底层把char 数组变成了byte[].)唯一不同的就是我们的StringBuffer内部操作方 ...
- Greenplum 调优--查看子节点SQL运行状态
摘自<Greenplum企业应用实战> 重点: 使用gp_dist_random函数,将查询下发到每个Segement 创建查看子节点SQL运行状态视图 1)创建v_active_sql视 ...
- CF620E New Year Tree 线段树+dfs序+bitset
线段树维护 dfs 序是显然的. 暴力建 60 个线段树太慢,于是用 bitset 优化就好了 ~ code: #include <bits/stdc++.h> #define M 63 ...
- 爬虫(三):Requests库的基本使用
一:什么是Requests Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库如果你看过上篇文章关于urllib库的使用,你会发现 ...
- F 阎小罗的Minimax (第十届山东理工大学ACM网络编程擂台赛 正式赛 )
题解:by Mercury_Lc 阎小罗的矩阵给的n和m都不超过300,枚举一下所有情况就可以了,用前缀和来储存.数组a[x][y]代表前x行前y列的和是多少,那么枚举每一种切割的方式就可以.注意一下 ...
- (转)libvirt和qemu编译安装
借鉴:https://www.cnblogs.com/grglym/p/8053553.html 借鉴:http://blog.chinaunix.net/uid-31410005-id-577189 ...
- wx.navigateTo的url不生效的问题
比如我要要从index页面跳转到logs. 在跳转的时候应该用switchTab,而不是wx.navigateTo 看api这句话 https://developers.weixin.qq.com/m ...