题目:给定一个数组array和一个值value,移除掉数组中所有与value值相等的元素,返回新的数组的长度;要求:不能分配额外的数组空间,且必须使用原地排序的思想,空间复杂度O(1);

举例:

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.

解题思路:

1.  首先找到第一个等于value和第一个不等于value的数的位置;

2.  等于value和不等于value之间的数则全部为value;

3.  每次交换时一定是第一个等于value和第一个不等于value的数进行交换;

举例说明:

3,3,3,2,2,3,1,2,4,3,4,3,2 ;  value = 3

1) 初始时:start = 0; index = 3; exchange两个位置的数,结果变为:2,3,3,3,2,3,1,2,4,3,4,3,2;

2) start = 1; index = 4; exchange: 2,2, 3,3,3,3,1,2,4,3,4,3,2;

3) start = 2; index = 5;因为nums[index] = 3,因此index一直递增,直到nums[index] != 3,此时index = 6; exchange: 2,2, 1,3,3,3,3,2,4,3,4,3,2;

4) start = 3; index = 7;exchange: 2,2, 1,2,3,3,3,3,4,3,4,3,2;

5) start = 4; index = 8; exchange: 2,2, 1,2,4,3,3,3,3,3,4,3,2;

6) start = 5; index = 9; nums[index] = 3, index递增,直到index = 10:exchange: 2,2, 1,2,4,4,3,3,3,3,3,3,2;

7) start = 6; index = 11; nums[index] = 3, index递增,直到index = 12: exchange: 2,2, 1,2,4,4,2,3,3,3,3,3,3;

此时start = 7;index = 12 等于数组长度,结束;

代码如下:

 public class Solution {
public int removeElement(int[] nums, int val) {
if(nums == null || nums.length < 1){
return 0;
}
int count = 0;
int index = 0;
while(index < nums.length && nums[index] != val){index ++; count ++;} //找到第一个等于value的数
int start = index;
while(index < nums.length && nums[index] == val){index ++;} //找到第一个不等于value的数
while(start < nums.length && index < nums.length){
exchange(nums, start, index);
count ++;
start ++;
while(index < nums.length && nums[index] == val){index ++;} }
return count;
}
public static void exchange(int[] nums, int index1, int index2)
{
int temp = nums[index1];
nums[index1] = nums[index2];
nums[index2] = temp;
}
}

Leetcode27--->Remove Element(移除数组中给定元素)的更多相关文章

  1. LeetCode 27 Remove Element (移除数组中指定元素)

    题目链接: https://leetcode.com/problems/remove-element/?tab=Description   Problem : 移除数组中给定target的元素,返回剩 ...

  2. js能力测评——移除数组中的元素

    移除数组中的元素 题目描述 : 移除数组 arr 中的所有值与 item 相等的元素.不要直接修改数组 arr,结果返回新的数组 示例1 输入 [1, 2, 3, 4, 2], 2 输出 [1, 3, ...

  3. LeetCode数组移除数组中目标元素等题目

    一种自己解题,一种高赞解题 /** * 移除数组中目标元素,返回新数组长度 * @param nums * @param val * @return */ public int removeEleme ...

  4. js小练习-移除数组中的元素

    移除数组 arr 中的所有值与 item 相等的元素,直接在给定的 arr 数组上进行操作,并将结果返回 代码: <!DOCTYPE HTML><html>    <he ...

  5. LeetCode 80 Remove Duplicates from Sorted Array II(移除数组中出现两次以上的元素)

    题目链接:https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/#/description 给定一个已经排好序的数组 ...

  6. array_unique() 函数移除数组中的重复的值

    array_unique() 函数移除数组中的重复的值,并返回结果数组. 当几个数组元素的值相等时,只保留第一个元素,其他的元素被删除. 返回的数组中键名不变.

  7. 【转载】C#通过Remove方法移除DataTable中的某一列数据

    在C#中的Datatable数据变量的操作过程中,有时候我们需要移除当前DataTable变量中的某一列的数据,此时我们就需要使用到DataTable变量内部的Columns属性变量的Remove方法 ...

  8. 移除数组中指定键(Yii2)

    /** * 移除数组中指定key * @param $data * @param $key * @return array */ public static function removeKey($d ...

  9. 【LeetCode每天一题】Find First and Last Position of Element in Sorted Array(找到排序数组中指定元素的开始和结束下标)

    Given an array of integers nums sorted in ascending order, find the starting and ending position of ...

随机推荐

  1. canvas雪花特效-jQuery插件实现

    这是一款效果十分逼真的html5 canvas下雪场景动画特效插件.这款下雪特效是基于Jason Brown的Snowfall jquery plugin的基础上制作的.在Snowfall jquer ...

  2. Vuforia切换回识别场景后黑屏解决

    使用Vuforia SDK开发时,如果从其他非识别场景切换回识别场景,可能会出现黑屏问题. 解决方法是在切换到其他场景时,先将当前场景的Tracker信息全部Stop.代码如下: IEnumerato ...

  3. uvm_reg_map——寄存器模型(八)

    所有的寄存器都需要地址,都需要加入到地址列表中 //-------------------------------------------------------------------------- ...

  4. iOS-浅谈runtime运行时机制02-runtime简单使用

    http://blog.csdn.net/jiajiayouba/article/details/44201079 由于OC是运行时语言,只有在程序运行时,才会去确定对象的类型,并调用类与对象相应的方 ...

  5. JMeter配置元件作用域

  6. mybatis association嵌套association的两级嵌套问题

    今天遇到了一个双表连接查询以及自关联的问题,由于第一次遇到,所以在这记下,日后好查阅 针对一个表的关联属性本身也有自关联的情况下,可以用association嵌套association的方法来处理. ...

  7. PAT (Basic Level) Practise (中文)- 1008. 数组元素循环右移问题 (20)

    一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN-1)变换为(AN-M …… AN-1 A0  ...

  8. LINQ中AsEnumerable与AsQueryable的区别

    AsEnumerable将一个序列向上转换为一个IEnumerable, 强制将Enumerable类下面的查询操作符绑定到后续的子查询当中:AsQueryable将一个序列向下转换为一个IQuery ...

  9. 监控电脑CPU,内存,文件大小,硬盘空间,IP,用户名

    public class MonitorTools { /// <summary> /// 获取具体进程的内存,线程等参数情况 /// </summary> /// <p ...

  10. IDEA设置每次打开重新选择项目

    通过这里,选择settings,或者进入之后的FILE->settings.搜索System 即可出现