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 ... 
随机推荐
- swift 基础-4
			函数:完成特定任务的代码块,通过名字来表示函数做什么 func 函数名(形参:形参类型)->返回类型 command +option+0 隐藏右边的框 //定义函数 func sayHello( ... 
- Android的bitmap和优化
			内存管理是个永恒的话题! 内存溢出:就是分配的内存不足以放下数据项序列.如在一个域中输入的数据超过了它的要求就会引发数据溢出问题,多余的数据就可以作为指令在计算机上运行.就是你要求分配的内存超出了系统 ... 
- Docker 给运行中的容器添加映射端口
			方法1 1.获得容器IP 将container_name 换成实际环境中的容器名 docker inspect `container_name` | grep IPAddress 2. iptable ... 
- js读取excel数据后的时间格式转换
			使用xlsx.full.min.js 获取excel的日期数据为:37858: 拿到的整数值是日期距离1900年1月1日的天数,这时需要写一个函数转换: function formatDate(num ... 
- mac不限速下载百度网盘
			本文转载自:https://blog.csdn.net/u010837612/article/details/80029212 相信大家都比较困惑,百度网盘客户端限速后一般只有几十K的下载速度,Win ... 
- 工作方法-scrum+番茄工作法
			1.产品和开发团队近期的工作分析和安排,使用scrum. 产品的工作:通过product backlog来列出 开发团队近期的工作安排:通过sprint backlog来列出,由个人认领,并估算(优先 ... 
- gd调试命令,gdb调试core文件
			使用 gcc -g test.c -o test.out 编译程序,只有加-g参数才支持gdb调试: 然后 gdb ./test.out 运行可执行文件,进入gdb调试模式(gdb),在括号后面的输入 ... 
- SharePoint 2013 安装配置(3-1)
			在第二部分中,我向您展示了如何在Windows Server 2012 R2 for SharePoint 2013上设置Active Directory域服务.现在我们应该能够在Active Dir ... 
- 【LeetCode】2.Add Two Numbers 链表数相加
			题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ... 
- hiho一下 第四十五周 博弈游戏·Nim游戏·二(转成NIm)
			Alice和Bob这一次准备玩一个关于硬币的游戏:N枚硬币排成一列,有的正面朝上,有的背面朝上,从左到右依次编号为1..N.现在两人轮流翻硬币,每次只能将一枚正面朝上的硬币翻过来,并且可以随自己的意愿 ... 
