Leetcode 27——Remove Element
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 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:
Given nums = [3,2,2,3], val = 3, Your function should return length = 2, with the first two elements of nums being 2. 分析:in-place就地remove元素,不允许分配另一个数组。可以这么想,允许用另一个数组的话,直接遍历这个数组,满足条件就添加到另一个数组中。但是不让用多一个数组的话,就用自身,因为满足条件的数量肯定是小于等于数组的长度,所以用一个index来计数即可,满足条件的放到index位置上,index++。
class Solution {
public int removeElement(int[] nums, int val) {
int index=0;
for(int i=0;i<nums.length;i++){
if(nums[i]!=val){
nums[index]=nums[i];
index++;
}
}
return index;
}
}
Leetcode 27——Remove Element的更多相关文章
- leetCode 27.Remove Element (删除元素) 解题思路和方法
Remove Element Given an array and a value, remove all instances of that value in place and return th ...
- LeetCode 27. Remove Element (移除元素)
Given an array and a value, remove all instances of that value in place and return the new length. D ...
- [LeetCode] 27. Remove Element 移除元素
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- LeetCode 27 Remove Element
Problem: Given an array and a value, remove all instances of that value in place and return the new ...
- Leetcode 27 Remove Element STL
和remove zero类似的方法完成该题 class Solution { public: int removeElement(vector<int>& nums, int va ...
- Java [leetcode 27]Remove Element
题目描述: Given an array and a value, remove all instances of that value in place and return the new len ...
- (双指针) leetcode 27. Remove Element
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- Leetcode 27. Remove Element(too easy)
Given an array and a value, remove all instances of that value in-place and return the new length. D ...
- [leetcode]27. Remove Element删除元素
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
随机推荐
- LAMP应用部署
LAMP+wordpress 部署博客 软件安装 yum -y install httpd yum -y install php yum -y install php-mysql yum -y ins ...
- 一篇关于PHP性能的文章
一篇关于PHP性能的文章 昨晚清理浏览器收藏夹网址时,发现了http://www.phpbench.com/,想起来应该是2015年发现的一个比较性能的文章,我就点进去看了看,发现还是全英文耶,刚好最 ...
- JavaScript设计模式(4)-桥接模式
桥接模式 在设计一个 Js API 时,可用来弱化它与使用它的类和对象之间的耦合 1. 事件监听器的回调函数 function getBeerById(id, callback) { asyncReq ...
- 转:SQL进阶之变量、事务、存储过程与触发器
一.变量那点事儿 1.1 局部变量 (1)声明局部变量 DECLARE @变量名 数据类型 DECLARE @name varchar(20) DECLARE @id int (2)为变量赋值 SET ...
- 使用Restify+superagent做数据转发
最近为了解决跨域问题,做了一个Node数据转发服务器,使用到了Restify和superagent. Restify 是nodejs的模块.虽然restify的API或多或少的参考了express,但 ...
- 【BZOJ2005】【NOI2010】能量采集(莫比乌斯反演,容斥原理)
[BZOJ2005][NOI2010]能量采集(莫比乌斯反演,容斥原理) 题面 Description 栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量.在这些植物采集能量 ...
- WPF自学入门(五)WPF依赖属性
在.NET中有事件也有属性,WPF中加入了路由事件,也加入了依赖属性.最近在写项目时还不知道WPF依赖属性是干什么用的,在使用依赖项属性的时候我都以为是在用.NET中的属性,但是确实上不是的,通过阅读 ...
- 测试一下OpenLiveWriter
$$ \sum_{0}^{n} k_i $$ OpenLiveWriter好丑啊啊啊啊,什么鬼?
- 网页中输出HTML代码
>>>>>> mustache.js的解决方案 var entityMap = { '&': '&', '<': '<', '>' ...
- MyBatis的三层级联和二层缓存
我们这里说的三层级联和二级缓存其实也是MyBatis映射器的知识点,只是因为比较难理解,所以单独拿出来讲解,下面是三层级联的内容: 我们知道,在数据库中包含着一对一,一对多 ...