• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

cynchanpin

  • 博客园
  • 联系
  • 订阅
  • 管理

View Post

LeetCode之RemoveElement

题目:

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);是计算两个地址间的距离,也就是元素个数

posted on 2017-07-02 19:31  cynchanpin  阅读(118)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3