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.  

思路分析:

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

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

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


C#代码: 

  1. public class Solution {
  2. public int RemoveElement(int[] nums, int val) {
  3. int j=nums.Length-,len= nums.Length,i=;
  4. while(j>=i){
  5. if(nums[j]==val){
  6. len--;
  7. j--;
  8. continue;
  9. }
  10. if(nums[i]==val){
  11. int temp = nums[i];
  12. nums[i] = nums[j];
  13. nums[j] = temp;
  14. len--;
  15. j--;
  16. }else{
  17. i++;
  18. }
  19. if(i==j){
  20. break;
  21. }
  22. }
  23. return len;
  24. }
  25. }

 或者:

  1. public class Solution {
  2. public int RemoveElement(int[] nums, int val) {
  3. int len= nums.Length;
  4. for(int i=;i<nums.Length;i++){
  5. if(i==len)break;
  6. if(nums[i]==val){
  7. if(nums[len-]==val){
  8. len--;
  9. i--;
  10. continue;
  11. }
  12. int temp = nums[i];
  13. nums[i] = nums[len-];
  14. nums[len-] = temp;
  15. len--;
  16. }
  17. }
  18. return len;
  19. }
  20. }

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. tomcat的配置

    配置tomcat需要 先下载JDK JDE配置环境http://jingyan.baidu.com/article/870c6fc33e62bcb03fe4be90.htmlXML配置 路径——> ...

  2. spring和UEditor结合

    前言 春节无聊,就去弄一下富文本编辑器,然后百度了一番,很多说百度的UEditor不错,然后去官网照着文档弄一遍,是挺简单好用的.然后想把这玩意结合到自己的一个spring项目里面,果然还是在点上传图 ...

  3. Javascript—①你好,世界!

    新手Perfect教程之Javascript教程①-你好,世界! 前言:不知道Javascript是什么东东的自行度娘或google一下 Javascript在html<head>和< ...

  4. C++/C语言程序代码

    //-----------------------------------1 #include <stdio.h> #include<stdlib.h> void main() ...

  5. Java Calendar.set 方法设置时间的问题

    因项目需要,需要遍历一年中的其中几个月,获得每个月的用户数量.  变量有:开始时间--startDate,结束时间--endDate. 逻辑很简单:获取到开始时间的月份和结束时间的月份,然后得到月份差 ...

  6. 了解 : 怎么处理jobbox status drop down list roll back

    that.onStatusEnumChange = function (toStatus) { //设计理念是当completed 和 rejected 的状态下,是无法换状态 if (toStatu ...

  7. 如何快速的学习selenium工具

    分享即快乐. 最近几年,软件测试工程师一度成为热门职业,作为测试员也是倍感压力.作为测试员来说,仅仅会手工测试让职业生涯陷入瓶颈.于是工作之余充电,学习了自动化测试工具selenium,打算进阶中高级 ...

  8. mfc--使用ShellExecute打开另一个可执行程序

    ShellExecute(sFile [, vArguments] [, vDirectory] [, vOperation] [, vShow]) ShellExecute(NULL,"o ...

  9. Java设计模式之《调停者模式》及应用场景

    原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/6518603.html 调停者模式. 我们想象一下这样的场景:一个系统内部通过许多的类互相之 ...

  10. SEO-外部链接类型以及标准

    外部链接 外链的作用:宣传你的网站 相信大家都听过"内链为王,外链为皇"这句话,不管这句话对不对,从这句话上面,我们都能体会到外链的重要性. 外链类型: 1.博客 2.论坛 3.分 ...