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 ...
随机推荐
- 关于swiper的滚动条滑动
<div class="swiper-container2"> <div class="swiper-wrapper"> <div ...
- IE浏览器兼容的常见问题及解决方案
常见6个问题及解决方案 1 IE6/IE7对display:inline-block的支持还欠缺 就是我们做导航栏时通常会用到<ul><li>标签去写,在现在一些主流的浏览器中 ...
- Tomcat下使用Druid配置JNDI数据源
com.alibaba.druid.pool.DruidDataSourceFactory实现了javax.naming.spi.ObjectFactory,可以作为JNDI数据源来配置. 一.下载D ...
- 【NOIP2016】蚯蚓(队列,单调性)
题目不再重复叙述 请参考: 洛谷 CJOJ 题解 先来说说非完美解法,也是我去年考场上的做法 考虑一下每一只蚯蚓增加的长度, 这个值并不需要每一次依次增加, 用一个变量维护即可,每次取出蚯蚓就加上这个 ...
- 关于Flex的DataGridColumn 的editorDataField属性
本人Flex菜鸟,纯纯的小白 最近在项目中提了一个需求,其实这个需求就是在原有的列表中接一列数据,只不过这一列数据是下拉列表选择的 最关键的是貌似以前的某一位大神已经添加过,我的工作就是调通,可是就是 ...
- Liunx2:Liunx目录结构
Liunx目录图 进入根目录,使用ll命令看一下Liunx整个根目录图: 这里面所有的目录都是买完服务器之后最初始的目录,没有进过任何加工.Liunx以树的结构组织所有目录,用一张图表示一下Liunx ...
- 【Webpack的使用指南 02】Webpack的常用解决方案
说是解决方案实际上更像是webpack的插件索引. 写这一篇的目的是为了形成一个所以,将来要用时直接来查找即可. 索引目录 1.自动构建HTML,可压缩空格,可给引用的js加版本号或随机数:html- ...
- 开发中使用Gson的实例(时间格式错误解决方法)
...... // 通过GSON解析,使用4个实体类来接受(TotalResponse.TradeRateResponse.TradeRatess.TbTradeRates) GsonBuilder ...
- 关于使用Ajax请求json数据,@RequestMapping返回中文乱码的几种解决办法
一.问题描述: 使用ajax请求json数据的时候,无论如何返回的响应编码都是ISO-8859-1类型,因为统一都是utf-8编码,导致出现返回结果中文乱码情况. $.ajax({ type:&quo ...
- 在linux上安装dotnetcore
dotnet core已经出来有一段时间了,不是什么新名词了.但这个技术,目前还是比较新的,企业也没有普遍应用.它最大的亮点就是跨平台,也就是我们写的c#代码,可以运行在linux上. 在国内学习do ...