题目:

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. ios 推送证书没有密钥 解决方案【转载】

    注意事项: 1.keychains选择Login 2.2.在创建完CertificateSigningRequest.certSigningRequest可以看到Keys中该有你的私有秘钥 3.按文档 ...

  2. POJ 1787 Charlie&#39;s Change

    多重背包 可行性+路径记录 题意是说你要用很多其它的零钱去买咖啡.最后输出你分别要用的 1,5 ,10 .25 的钱的数量. 多重背包二进制分解.然后记录下 这个状态.最后逆向推就可以. #inclu ...

  3. Android异步处理二:使用AsyncTask异步更新UI界面

    在<Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面>中,我们使用Thread+Handler的方式实现了异步更新UI界面,这一篇中,我们介绍一种更为简 ...

  4. 苹果开发之COCOA编程(第三版)上半部分

    第一章:什么是Cocoa 1.1 历史简介 1.2 开发工具:Xcode.Interface Builder(一个GUI构建工具).在它们内部,使用gcc为编译器来编译代码,并使用gdb来查找错误 1 ...

  5. 谁能举个通俗易懂的例子告诉我IAAS,SAAS,PAAS的区别?【转自知乎】

    是时候祭出这篇吃货文章了: ———————————————————— ———————————————————— ———————————————————— &amp;amp;amp;amp;lt ...

  6. quick-cocos2d-x 系列之——环境搭建(Mac版)

    quick-cocos2d-x简单介绍 何为quick-cocos2d-x? ?   简单一句话:quick-cocos2d-x是採用lua语言,通过tolua++工具对cocos2d-x进一步封装, ...

  7. Linux下/usr/bin/python被删除的后果

    可能部分的人使用linux都有直接root登陆的习惯,这有很大的便利性,因为很多的命令不需要使用sudo请求root权限.但是使用root权限,所有的命令都会立即被执行,即使这个命令是对系统有害处的. ...

  8. 《程序员代码面试指南》第八章 数组和矩阵问题 数组的partition 调整

    题目 数组的partition 调整 java代码 package com.lizhouwei.chapter8; /** * @Description: 数组的partition 调整 * @Aut ...

  9. Canvas动画按钮

    在线演示 本地下载

  10. ubuntu tomcat 配置及使用细节

    1.改端口号(两个) vi server.xml 一个是http协议端口  <Connector port="8091" protocol="HTTP/1.1&qu ...