LeetCode OJ 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 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.
Hint:
- Try two pointers.
- Did you use the property of "the order of elements can be changed"?
- What happens when the elements to remove are rare?
Subscribe to see which companies asked this question
【思路】
由于返回是数组的数字顺序可以改变,我们可以从后往前遍历。如果遇到的值和给定的val相同,则数组长度len-1,指针向前移动。如果遇到的值和val不同,则向前找到第一个和val值相同的数组元素,然后把这个元素和最后的元素交换位置。len = len - 1,尾指针继续向前移动。
举个例子,初始数组[3,2,2,3,4,3,5,5,3] val = 3
1. 尾指针 notval = len - 1 = 8,此时nums[notval] = 3,则指针向前移动,len - 1,数组变为[3,2,2,3,4,3,5,5]
2. notval = 7,nums[7] = 5 != 3,向前找到第一个等于3的数组元素,nums[5] = 3,此时令nums[5] = nums[7],len - 1,数组变为[3,2,2,3,4,5,5]
3. 重复上述过程,直到向前找不到值为val的数组元素,则可以返回[5,2,2,5,4,5]。
代码如下:
public class Solution {
public int removeElement(int[] nums, int val) {
if(nums==null || nums.length==0) return 0;
int len = nums.length;
int notval, isval;
for(notval = len - 1; notval >=0; notval--){
if(nums[notval] == val) len = len - 1;
else{
for(isval = notval - 1; isval >= 0; isval--)
if(nums[isval] == val) break;
if(isval < 0) return len;
else{
nums[isval] = nums[notval];
len = len - 1;
}
}
}
return len;
}
}
LeetCode OJ 27. Remove Element的更多相关文章
- [Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
- C# 写 LeetCode easy #27 Remove Element
27. Remove Element Given an array nums and a value val, remove all instances of that value in-place ...
- 【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】27 - Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- 【一天一道LeetCode】#27. Remove Element
一天一道LeetCode系列 (一)题目 Given an array and a value, remove all instances of that value in place and ret ...
- Leetcode No.27 Remove Element(c++实现)
1. 题目 1.1 英文题目 Given an integer array nums and an integer val, remove all occurrences of val in nums ...
- 【LeetCode】27. Remove Element 解题报告(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 记录起始位置 日期 题目地址:https:/ ...
- 【LeetCode OJ】Remove Element
题目:Given an array and a value, remove all instances of that value in place and return the new length ...
- LeetCode OJ:Remove Element(移除元素)
Given an array and a value, remove all instances of that value in place and return the new length. T ...
随机推荐
- openstack添加数据库
输入:neutron-db-manage revision -m "表的名称" neutron-db-manage upgrade head 如果遇到版本名找不到的情 ...
- 《JavaScript高级程序设计》读书笔记 ---RegExp 类型
ECMAScript 通过RegExp 类型来支持正则表达式.使用下面类似Perl 的语法,就可以创建一个正则表达式.var expression = / pattern / flags ; 其中的模 ...
- 新手站长选择WordPress程序建站需要注意的8个问题
文章出自:http://www.banwagongvps.com/119.html 如今我们不论是出于个人的兴趣爱好,还是出于我们希望通过搭建自己的网站获利的动机,入门级别的都变得非 常的简单,我们只 ...
- OGG中断后,重新同步操作
模拟一下goldengata中断后,重新同步操作: 1.关掉源端抽取进程 GGSCI (20081122-2105) 15> info all Program Status Group Lag ...
- Paint Pearls
Paint Pearls 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5009 dp+双向链表优化 看到题目,很自然地可以定义状态:dp[i]表示涂好 ...
- 第一百零五节,JavaScript正则表达式
JavaScript正则表达式 学习要点: 1.什么是正则表达式 2.创建正则表达式 3.获取控制 4.常用的正则 假设用户需要在HTML表单中填写姓名.地址.出生日期等.那么在将表单提交到服务器进一 ...
- Struts2-1.配置&与第一个应用
配置流程 1.web项目中导入Strus2应用需要的包,复制到项目的lib文件夹下 点击此处下载需要的包,解压后复制进去即可:http://pan.baidu.com/s/1jHhjd2Y 2.编写S ...
- 布局常见问题之css实现多行文本溢出显示省略号(…)全攻略
省略号在ie中可以使用text-overflow:ellipsis了,但有很多的浏览器都需要固定宽度了,同时ff这些浏览器并不支持text-overflow:ellipsis设置了,下文来给各位整理一 ...
- VS2013使用EF与mysql数据库.
一个VS2013的mvc+EF+mysql的项目,需要连接Mysql数据库 一,下载一个mysql-for-visualstudio-1.2.3.msi,在自己的电脑上安装,这个是解决在创建实体模型( ...
- bzoj4318: OSU!&&CF235BLet's Play Osu!
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4318 4318: OSU! Time Limit: 2 Sec Memory Limit ...