题目:

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

  

题解:

  这个题和之前的题类似,难度不大

Solution 1 ()

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

Solution 2 ()

class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int len = ;
for (int i = ; i < nums.size(); ++i) {
if (nums[i] != val) nums[len++] = nums[i];
}
return len;
}
};

【LeetCode】027. Remove Element的更多相关文章

  1. 【LeetCode】27. Remove Element (2 solutions)

    Remove Element Given an array and a value, remove all instances of that value in place and return th ...

  2. 【LeetCode】27 - Remove Element

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

  3. 【LeetCode】27. Remove Element 解题报告(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 记录起始位置 日期 题目地址:https:/ ...

  4. 【LeetCode】402. Remove K Digits 解题报告(Python)

    [LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

  5. 【LeetCode】722. Remove Comments 解题报告(Python)

    [LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...

  6. 【一天一道LeetCode】#27. Remove Element

    一天一道LeetCode系列 (一)题目 Given an array and a value, remove all instances of that value in place and ret ...

  7. 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...

  8. 【LeetCode】83 - Remove Duplicates from Sorted Array

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  9. 【LeetCode】26. Remove Duplicates from Sorted Array

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

随机推荐

  1. CentOS6.5下Oracle11G-R2安装、卸载

    CentOS6.5下Oracle11G-R2安装.卸载 资源下载地址(包含本人全部安装过程中,系统备份文件):http://download.csdn.net/detail/attagain/7700 ...

  2. 开始nodejs+express的学习+实践(1)

    开始nodejs+express的学习+实践(1) 开始nodejs+express的学习+实践(2) 开始nodejs+express的学习+实践(3) 开始nodejs+express的学习+实践 ...

  3. 必会必知git

    git必会必知   1 前言 git前身是BitKeeper,但是他不是开源软件,不符合当时开源趋势,于是就会有了开源的git,git开发只用了十天时间.目前git是公司开发必不可少的一个工具,用于多 ...

  4. erlang的token值加解密

    对于加解密,需客户端和服务器制定好对应的规则(如:加密算法(aes,des等).加密模式(cbc,cfb)),去加密,再按逆序列解密.这里的key是根据数字.大小写字母.符合组合的,每次请求获取一个动 ...

  5. 总是想把Linux服务器上的重要文件备份到本地,在此转一篇实现windows和linux互传文件的文章

    尝试从windows xp向ubuntu11.10传文件 ubuntu使用的是ssh windows使用的是putty和其附带的pscp 首先配置ubuntu: 1.先使用netstat -tl或se ...

  6. rtmp直播拉流客户端EasyRTMPClient设计过程中时间戳问题汇总

    EasyRTMPClient 简介 EasyRTMPClient是EasyDarwin流媒体团队开发.提供的一套非常稳定.易用.支持重连接的RTMPClient工具,以SDK形式提供,接口调用非常简单 ...

  7. 基于EasyDarwin框架实现EasyNVR H5无插件直播流媒体服务器方案

    在之前的一篇博客<web无插件播放RTSP摄像机方案,拒绝插件,拥抱H5!>中,描述了实现一套H5无插件直播方案的各个组件的参考建议,又在博客<EasyNVR H5流媒体服务器方案架 ...

  8. SQL Server里的 ISNULL 与 NULLIF(转)

    SQL Server 中有两个参数,语法:     ISNULL(check_expression, replacement_value) check_expression 与 replacement ...

  9. SASL mechanism

    <property> <name>hive.spark.client.rpc.sasl.mechanisms</name> <value>DIGEST- ...

  10. 我的Android进阶之旅------>真正在公司看几天代码的感触

    仅以此文来回顾这一周我的工作情况,以及由此而触发的感想. ============================================================= 来到新公司5天了, ...