1. Remove Element My Submissions QuestionEditorial Solution

    Total Accepted: 115367 Total Submissions: 340963 Difficulty: Easy

    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.

思路:如下,一碰到指定的val,dis++,dis代表当前元素非val得时候向前移动的距离

class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int n=nums.size();
int i=0,dis=0;
while(i<n){
if(nums[i]==val)dis++;
else nums[i-dis]=nums[i];
i++;
}
return n-dis;
}
};

最新文章

  1. SqlServer英文单词全字匹配
  2. 1.NSNotification|远程通知|本地通知|激光推送
  3. NoSQL生态系统——类似Bigtable列存储,或者Dynamo的key存储(kv存储如BDB,结构化存储如redis,文档存储如mongoDB)
  4. net软件自动生成开发编程框架编程机器人
  5. modelsim do文件仿真
  6. 沈逸老师PHP魔鬼特训笔记(10)
  7. vb.net之窗体继承
  8. ansilbe 入门001、ansible的介绍
  9. jquery ajax协调SpringMVCD实现局部刷新IV
  10. 一步一步学Vue(二)
  11. 第2章 rsync(一):基本命令和用法
  12. Linux学习——shell编程之运算符
  13. WebClient下载文件
  14. spring从服务器磁盘读取图片,然后显示于前端页面上
  15. Java JDBC调用存储过程:无参、输入带参、输出及输出带参
  16. PHP中PSR-[0-4]规范(转)
  17. day 18 类,对象
  18. .NetCore下使用Prometheus实现系统监控和警报 (四)客户端代码处理
  19. 编译 php-memcache 扩展时提示Cannot find autoconf
  20. BleedTree动画混合树[Unity]

热门文章

  1. ruby基本图片上传
  2. 21.7.24 test
  3. VMware Workstation 16.2 Pro for Linux SLIC 2.6 &amp; Unlocker
  4. 手把手教你学Dapr - 3. 使用Dapr运行第一个.Net程序
  5. hdu 2795 Billboard(单点更新,区间查询)
  6. 攻防环境配置大全(iss/apache/nginx/tomcat/jboss/weblogic)
  7. HDC2021:HMS Core分析服务,数智化营销闭环方案帮助开发者实现精益增长
  8. Qt 项目管理文件(.pro) 详解
  9. robot_framewok自动化测试--(5)Screenshot 库
  10. Linux常用命令和快捷键整理:(1)常用命令