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 ...
随机推荐
- CentOS安装glibc-2.14,错误安装libc.so.6丢失急救办法
CentOS安装glibc-2.14,错误安装libc.so.6丢失急救办法 到http://ftp.gnu.org/gnu/glibc/下载glibc-2.14.tar.xz tar glibc ...
- Android Studio 中提示 Private field 'mType' is assigned but never accessed 的原因
Android Studio 是个很酷的编译器,之前发现有个代码提示很奇怪,但无奈一直没看懂他的意思,不过也没报错就没太在意,刚刚突然领悟了,原来是自己代码不规范. Private field 'mT ...
- 湖南多校对抗赛(2015.05.03)Problem A: Twenty-four point
给四个数 问能不能算出24点...我的方法比较烂...920ms 差点TLE.应该有更好的方法. #include<stdio.h> #include<string.h> #i ...
- 网站常用js代码搜集
1.若是手机端打开,则跳转到手机页面 <script language="javascript"> if(navigator.userAgent.match(/(iPh ...
- iOS开发之即时通讯之Socket(AsyncSocket)
1.AsyncSocket介绍 如果需要在项目中像QQ微信一样做到即时通讯,必须使用socket通讯. iOS中Socket编程的方式: BSD Socket: BSD Socket 是UNIX系统中 ...
- uhttpd配置文件分析
文件位于 /etc/config/uhttpd. root@hbg:/etc/config# cat uhttpd config uhttpd 'main' list listen_ht ...
- hdu 1210 Eddy's 洗牌问题
Problem Description Eddy是个ACMer,他不仅喜欢做ACM题,而且对于纸牌也有一定的研究,他在无聊时研究发现,如果他有2N张牌,编号为1,2,3..n,n+1,..2n.这也是 ...
- 第三次冲刺spring会议(第六次会议)
[例会时间]2014/5/25 21:15 [例会地点]9#446 [例会形式]轮流发言 [例会主持]马翔 [例会记录]兰梦 小组成员:兰梦 ,马翔,李金吉,赵天,胡佳奇
- Just do it!!!
4月英语竞赛期中考试 5月初级程序员考试 6月英语四级考试 7月期末考试 加油吧年轻人!
- TFS 2012使用简介
为什么使用TFS 2012进行源代码管理——TFS 2012使用简介(一) 来源:雪雁 http://www.cnblogs.com/codelove/archive/2013/03/16/2963 ...