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 ...
随机推荐
- 旋转数组中的最小数字,剑指offer,P70 二分查找来实现O(logn)的查找
public class MinNumberInRotatedArray { public int getMinNumInRotatedArray(int[] array) { if(array == ...
- OC 调用JS 代码 处理HTML5 实战
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; ...
- CBC之php java兼容版本
网上搜了N多代码,都是你抄我的,我抄你的,着实让人无语对苍天.经过多番资料的查找,php与java的cbc加密.解密结果终于一致了,代码如下: Java加密解密类: package main; imp ...
- 5个可用提高Godaddy主机速度的第三方CDN加速服务商
毕竟Godaddy主机数据中心位于美国.新加坡.欧洲三个数据中心,一般我们都会选择美国数据中心,相比较其他2个机房的速度是快和稳定的,很多人要说为什么新加坡数据中心速度不好呢?因为目前的新加坡机房不是 ...
- 【第五篇】Volley代码修改之图片二级缓存以及相关源码阅读(重写ImageLoader.ImageCache)
前面http://www.cnblogs.com/androidsuperman/p/8a157b18ede85caa61ca5bc04bba43d0.html 有讲到使用LRU来处理缓存的,但是只是 ...
- vb.net_一个半成品
Imports System.Text Imports System.Runtime.InteropServices Public Class Form1 '引用win32api进行枚举窗体句柄操作 ...
- w3c School
w3c School : http://www.w3school.com.cn/h.asp
- Node.js:常用工具util
概要:本篇博客的主要内容是介绍node.js的常用工具util. 1.util.inherits util.inherits(constructor,superConstructor)是一个实现对象间 ...
- jquery实现页面内部的内容切换
html页面 .box-body-1-3 li{ margin: 20px; cursor: pointer; //实现鼠标放在上面是小手状态 } 点击列表 <div class=" ...
- Python之生产者&、消费者模型
多线程中的生产者和消费者模型: 生产者和消费者可以用多线程实现,它们通过Queue队列进行通信. import time,random import Queue,threading q = Queue ...