Description: 

  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.


 

思路分析:

  1.最终要求的是输出除给定值以外的其他数组中的值,所写方法需要返回的是这些值的总个数,即输入[3,2,2,3]==>输出:2,并且数组的新状态为[2,2,3,3];

  2.最近在复习排序算法,所以这个问题很亲切,其中也一种排序的影子,只不过是把特定的值而不是最大值放到最后,问题还有一个约束:只能用常数级的额外空间,即O(n)=1,所以不能通过额外的数组来记录给定值以外的值完成;

  3.因此,排序里面每一次循环里怎么把局部最大值移动到局部的最后,这里也可沿用,只不过,这里问题有其特殊性:值一旦已经确定了,不需要后续的一轮比较来确定(排序的时候最大值需要这样来确定),一旦遍历到就可以直接当做‘最大值’来移动到最左边,而且最大值只有一个,但特定值可以同时出现。


C#代码: 

 public class Solution {
public int RemoveElement(int[] nums, int val) {
int j=nums.Length-,len= nums.Length,i=;
while(j>=i){
if(nums[j]==val){
len--;
j--;
continue;
}
if(nums[i]==val){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
len--;
j--;
}else{
i++;
}
if(i==j){
break;
}
}
return len;
}
}

 或者:

public class Solution {
public int RemoveElement(int[] nums, int val) {
int len= nums.Length;
for(int i=;i<nums.Length;i++){
if(i==len)break;
if(nums[i]==val){
if(nums[len-]==val){
len--;
i--;
continue;
}
int temp = nums[i];
nums[i] = nums[len-];
nums[len-] = temp;
len--;
}
}
return len;
}
}

27. Remove Element - 移除元素-Easy的更多相关文章

  1. [LeetCode] 27. Remove Element 移除元素

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  2. [LeetCode]27. Remove Element移除元素

    Given an array nums and a value val, remove all instances of that value in-place and return the new ...

  3. [LeetCode] Remove Element 移除元素

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  4. 027 Remove Element 移除元素

    给定一个数组和一个值,在这个数组中原地移除指定值和返回移除后新的数组长度.不要为其他数组分配额外空间,你必须使用 O(1) 的额外内存原地修改这个输入数组.元素的顺序可以改变.超过返回的新的数组长度以 ...

  5. 27. Remove Element【easy】

    27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...

  6. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  7. [Leetcode][Python]27: Remove Element

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...

  8. leetCode练题——27. Remove Element

    1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...

  9. 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  ...

随机推荐

  1. mdadm命令详解及实验过程

    一.概念 mdadm是multiple devices admin的简称,它是Linux下的一款标准的软件 RAID 管理工具,作者是Neil Brown 二.特点 mdadm能够诊断.监控和收集详细 ...

  2. 谈谈JavaScript代码混淆

    概述: 案例:Cesium打包流程,相关技术点和大概流程 原理:代码优化的意义:压缩 优化 混淆 优化:如何完善Cesium打包流程 关键字:Cesium gulp uglifyjs 字数:2330 ...

  3. ubuntu下包管理器apt-get常用命令

    apt-cache search package 搜索包 apt-cache show package 获取包的相关信息,如说明.大小.版本等 sudo apt-get install package ...

  4. 前端总结·基础篇·CSS(一)布局

    目录 这是<前端总结·基础篇·CSS>系列的第一篇,主要总结一下布局的基础知识. 一.显示(display) 1.1 盒模型(box-model) 1.2 行内元素(inline) &am ...

  5. phpcms代码读取文章的内容 实用可行的方法

    在使用phpcms做网站的时候经常遇到读取网站的内容作为推荐,而不是描述.这里使用可行的方法交你如何读取内容推荐.方法有两个,第一种执行的效率低,第二个效率高些. 1. {pc:get sql=&qu ...

  6. Hibernate框架双向多对多关联映射关系

    建立双向多对多关联关系    Project.java (项目表)                private Integer proid;                private Strin ...

  7. 提交Sublime Text 插件到Package Control

    最近写了一个lua智能提示的插件LuaSmartTips.这个插件一直都是自己一个人在用,昨天突然想把插件提交到Package Control,如果其他的人有这样的需求就可以直接安装. Package ...

  8. 数据库-增删改查操作SQL实现

    一.数据插入-Insert 1. 插入单条记录 insert into 表名(字段名,字段名,字段名) //当插入所有字段时,字段名可以省略 values('值1','值2','值3'); 2. 插入 ...

  9. 测试不同格式下depth buffer的精度

    这篇文章主要是参考MJP的“Attack of The Depth Buffer”,测试不同格式下depth buffer的精度. 测试的depth buffer包含两类: 一是非线性的depth b ...

  10. 配置apache,及mac下安装mysql

    先打开apache,在浏览器上输入  localhost     回车后会如果屏幕上显示:It works! 如下图: 这说明你的apache已开启 在window下配置apache: 1.找到apa ...