题目:给定一个数组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. 关于vue-resource 转变成axios的过程

    在做东钿贷后系统的时候,我选择了vue-resource这个插件作为与服务器沟通工具,但是听说前端同行说vuejs2.0已经不在维护vue-resource了,vuejs2.0 已经使用了axios了 ...

  2. <Android 应用 之路> 天气预报(三)

    昨天介绍了基本的载入界面,今天介绍下天气信息显示界面的代码 基本ListView显示 搜索框,查询城市 上一篇文章中,载入界面通过showWeatherInfo()方法跳转到天气信息显示界面 priv ...

  3. HDU 2149 Public Sale 拍卖(巴什博弈)

    思路:只要能给对方留下n+1,我就能胜,否则败. #include <iostream> #include <cstdio> using namespace std; int ...

  4. This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms while caching

    今天运行自己的网站时报了这样一个错误,很是纳闷,这个网站运行了这么久,怎么报这个错呢,原来是做缓存的时候用到了基于windows平台的加密算法.解决方法如下: 删除注册表下的这个节点即可.删除HKEY ...

  5. python爬虫之路——初识数据库存储

    非关系型数据库:MongoDB.关系型数据库:MySQL 关系型和非关系型的区别: 安装: 使用: 应用场景: mongoDB是一种非关系型数据库,分为四大类:键值存储数据库,列存储数据库,文档型数据 ...

  6. 使用HelpProvide组件调用帮助文件

    实现效果: 知识运用: HelpProvider组件的HelpNameSpace属性 //于对象关联的帮助文件名 public virtual string HelpNameSpace {get; s ...

  7. Visual Studio Professional 2015 简体中文专业版 序列号

    Visual Studio Professional 2015 简体中文专业版 专业版激活密钥:HMGNV-WCYXV-X7G9W-YCX63-B98R2 Visual Studio Enterpri ...

  8. javaweb基础(19)_jsp标签

    一.JSP标签介绍 JSP标签也称之为Jsp Action(JSP动作)元素,它用于在Jsp页面中提供业务逻辑功能,避免在JSP页面中直接编写java代码,造成jsp页面难以维护. 二.JSP常用标签 ...

  9. Bootstrap历练实例:动画的进度条

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  10. Bootstrap历练实例:标签修饰

    您可以使用修饰的 class label-default.label-primary.label-success.label-info.label-warning.label-danger 来改变标签 ...