Leetcode27--->Remove Element(移除数组中给定元素)
题目:给定一个数组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(移除数组中给定元素)的更多相关文章
- LeetCode 27 Remove Element (移除数组中指定元素)
题目链接: https://leetcode.com/problems/remove-element/?tab=Description Problem : 移除数组中给定target的元素,返回剩 ...
- js能力测评——移除数组中的元素
移除数组中的元素 题目描述 : 移除数组 arr 中的所有值与 item 相等的元素.不要直接修改数组 arr,结果返回新的数组 示例1 输入 [1, 2, 3, 4, 2], 2 输出 [1, 3, ...
- LeetCode数组移除数组中目标元素等题目
一种自己解题,一种高赞解题 /** * 移除数组中目标元素,返回新数组长度 * @param nums * @param val * @return */ public int removeEleme ...
- js小练习-移除数组中的元素
移除数组 arr 中的所有值与 item 相等的元素,直接在给定的 arr 数组上进行操作,并将结果返回 代码: <!DOCTYPE HTML><html> <he ...
- LeetCode 80 Remove Duplicates from Sorted Array II(移除数组中出现两次以上的元素)
题目链接:https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/#/description 给定一个已经排好序的数组 ...
- array_unique() 函数移除数组中的重复的值
array_unique() 函数移除数组中的重复的值,并返回结果数组. 当几个数组元素的值相等时,只保留第一个元素,其他的元素被删除. 返回的数组中键名不变.
- 【转载】C#通过Remove方法移除DataTable中的某一列数据
在C#中的Datatable数据变量的操作过程中,有时候我们需要移除当前DataTable变量中的某一列的数据,此时我们就需要使用到DataTable变量内部的Columns属性变量的Remove方法 ...
- 移除数组中指定键(Yii2)
/** * 移除数组中指定key * @param $data * @param $key * @return array */ public static function removeKey($d ...
- 【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 ...
随机推荐
- [20190620]日常学习记录(三)-初识promise及vuex
在学习promise之前重温了Ajax的原生js实现, 在原生js中发送一个http请求首先new XMLHttpRequest() 然后定义状态变更事件 浏览器监听请求的状态,触发不同状态下相应的代 ...
- Appium基础五:appium相关API
1.获取信息类: 1.1 获取当前界面的组件: driver.currentActivity(); //获取当前界面的activity,可用于断言是否跳转到预期的activity 1.2 获取当前页面 ...
- mui对话框、表单
1.mui.alert() 普通提醒参数 1.message Type: String 提示对话框上显示的内容 2.title Type: String 提示对话框上显示的标题 3.btnValue ...
- Ubuntu 自动获取ip地址
$ sudo dhclient -r //release ip 释放IP$ sudo dhclient //获取IP手動使用 DHCP 自 ...
- Android笔记--View绘制流程源码分析(一)
Android笔记--View绘制流程源码分析 View绘制之前框架流程分析 View绘制的分析始终是离不开Activity及其内部的Window的.在Activity的源码启动流程中,一并包含 着A ...
- Integer的一个小问题
看面试题的时候看到这道题: public class Demo { public static void main(String[] args) { Integer i1 = 128; Integer ...
- Servlet中的属性(attribute)和参数(parameter)的区别
1.引子 初学者对属性(attribute)和参数(parameter)容易搞混.没搞清他们的区别,项目中就可能出现一此莫名其妙的问题. 2.两者的区别 1) 属性(attribute) 属性是在后台 ...
- Ubuntu系统Apache 2部署SSL证书
几天前用Apache 2部署了一个静态网页,但通过域名访问时Google提示“不安全”,经了解,原来是缺少证书. 什么是SSL证书? SSL 是指安全套接字层,简而言之,它是一项标准技术,可确保互联网 ...
- 使用EventLog组件保存Windows系统日志
实现效果: 知识运用: EventLog类的CreateEventSource方法 //用于建立一个应用程序 使用指定的Sourc作为向本机上的日志中写入日志项的有效事件源 CreateEventS ...
- BCB:AnsiString BSTR WideString
WideString wstr;AnsiString astr;wchar_t *wp;//或者 BSTR wp; wp=wstr.c_bstr(); //WideString转化为BSTRwstr= ...