题目:

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

The order of elements can be changed. It doesn’t matter what you leave beyond the new length.

分析:

要求,移除具有n个元素的数组中全部指定的数字,返回删除后的数组长度。

看似简单。事实上也能体现一个人的编程水平。

解法1是优化后的,解法2是參照网上的STL解法。记录下来。

代码:

解释一下STL的几个算法。都包括在algorithm中:
  1. random_shuffle(a,a+20);是将数组元素随机打乱。

  2. remove(A,A+n,elem);是移除数组中elem元素,可是并没有把空间缩小,要缩小虚要用erase方法
  3. distance(A,A+n);是计算两个地址间的距离,也就是元素个数

LeetCode之RemoveElement的更多相关文章

  1. leetcode — remove-element

    /** * Source : https://oj.leetcode.com/problems/remove-element/ * * Created by lverpeng on 2017/7/12 ...

  2. [LeetCode] Remove Element 移除元素

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

  3. Leetcode分类刷题答案&心得

    Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...

  4. leetcode算法分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  5. LeetCode Remove Element

    原题链接在这里:https://leetcode.com/problems/remove-element/ 题目: Given an array and a value, remove all ins ...

  6. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  7. LeetCode OJ-- Remove Element

    https://oj.leetcode.com/problems/remove-element/ 简单处理 class Solution { public: int removeElement(int ...

  8. LeetCode题目分类

    利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...

  9. LeetCode OJ 题解

    博客搬至blog.csgrandeur.com,cnblogs不再更新. 新的题解会更新在新博客:http://blog.csgrandeur.com/2014/01/15/LeetCode-OJ-S ...

随机推荐

  1. RMAN 备份与恢复 实例

    1. 检查数据库模式:    sqlplus /nolog     conn /as sysdba    archive log list (查看数据库是否处于归档模式中) 若为非归档,则修改数据库归 ...

  2. 软件-版本控制:VCS(版本控制系统)

    ylbtech-软件-版本控制:VCS(版本控制系统) 版本控制系统(Version Control System),是一种记录一个或若干文件内容变化,以便将来查阅特定版本修订情况的系统.版本控制系统 ...

  3. Genuitec

  4. 第7章 Android中访问网络资源

    http://developer.android.com/index.html->https://developer.android.com/index.html https://develop ...

  5. TPshop规格组合错误

    TPshop规格组合错误 修改: admin/logic/goodslogic.class.php 中 方法:getSpecInput() 中 asort($spec_arr_sort) 去掉

  6. Scala学习2 ———— 三种方式完成HelloWorld程序

    三种方式完成HelloWorld程序 分别采用在REPL,命令行(scala脚本)和Eclipse下运行hello world. 一.Scala REPL. 按照第一篇在windows下安装好scal ...

  7. 5.20 mybatis反向生成的映射文件xml(如果需要自己定义其他sql语句时如下)

    解决mybatis-generator 生成的mapper.xml覆盖自定义sql的问题 mybatis-generator是个好工具,一建即可生成基本增删改成功能的mapper.xml.但这些是不够 ...

  8. 【PostgreSQL-9.6.3】psql常用命令

    命令 描述 \l 查看数据库 \c 换库 \d 查看所有表 \dt 只显示匹配的表 \di 只显示匹配的索引 \ds 只显示匹配的序列 \dv 只显示匹配的视图 \df 只显示匹配的函数 \d t1 ...

  9. Eclipse Rap开发 异步刷新UI处理

    1.Display.getCurrent()获取的是当前线程的display对象,如果当前在非UI线程中那么获取到的display对象为空:      一般Display.getCurrent() 用 ...

  10. Tinyxml2学习

    转自http://www.360doc.com/content/13/1223/16/3684846_339528825.shtml,尊重原文 什么是XML? XML全称EXtensible Mark ...