【LeetCode】027. Remove Element
题目:
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的更多相关文章
- 【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 ...
- 【LeetCode】27 - Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- 【LeetCode】27. Remove Element 解题报告(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 记录起始位置 日期 题目地址:https:/ ...
- 【LeetCode】402. Remove K Digits 解题报告(Python)
[LeetCode]402. Remove K Digits 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...
- 【LeetCode】722. Remove Comments 解题报告(Python)
[LeetCode]722. Remove Comments 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/remove-c ...
- 【一天一道LeetCode】#27. Remove Element
一天一道LeetCode系列 (一)题目 Given an array and a value, remove all instances of that value in place and ret ...
- 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...
- 【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 ...
- 【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 ...
随机推荐
- Laravel 5.4的本地化
简介 Laravel 的本地化功能提供方便的方法来获取多语言的字符串,让你的网站可以简单的支持多语言. 语言包存放在 resources/lang 目录下的文件里.在此目录中应该有应用对应支持的语言并 ...
- javascript的slice(),splice(),split(),substring(),substr()
例子摘抄于http://www.w3school.com.cn/jsref/jsref_obj_array.asp 1.slice(): Array和String对象都有 在Array中 slice ...
- unbuntu16.04上python开发环境搭建建议
unbuntu16.04上python开发环境搭建建议 2017-12-20 10:39:27 推荐列表: pycharm: 可以自行破解,但是不推荐,另外也不稳定 pydev+eclipse: ...
- ios 推送证书没有密钥 解决方案【转载】
注意事项: 1.keychains选择Login 2.2.在创建完CertificateSigningRequest.certSigningRequest可以看到Keys中该有你的私有秘钥 3.按文档 ...
- HTML经典标签用法
1.marquee属性的使用说明 <marquee> ... </marquee>移动属性的设置 ,这种移动不仅仅局限于文字,也可以应用于图片,表格等等 鼠标属性 onMo ...
- Python学习总结之三 -- 优雅的字符串
优雅的字符串 前言 记得我在Python学习总结第一篇中有提到字符串,那个可以算是先打个招呼吧,因为没有提到任何关于字符串的处理方法.今天,给大家详细讲解一下Python中字符串的使用方法,如有不当或 ...
- 初学shell,今天遇到由wget下载到本地的网页源代码的乱码问题,无聊的写了一个转码的脚本
今天用wget想下载文件,结果下载了一堆本地的index.html文件,一查看全是乱码,然后百度了一下,网页的编码格式大概有三种: 1.utf-8 2.gb2312 3.gbk 要在网页源码中的< ...
- 对EasyDarwin开源项目后续发展的思考:站在巨人的肩膀上再跳上另一个更高的肩膀
2017 EasyDarwin现状 自从2012年EasyDarwin项目创立开始,经过了快5年了,时光飞逝,如今EasyDarwin已经发展成为了不仅仅是一个单纯的开源流媒体服务器项目了,已经是各种 ...
- Golang 环境变量及工作区概念
GOROOT go的安装路径 GOPATH 可以有多个目录,每个目录就是一个工作区,放置源码文件,以及安装后的归档文件和可执行文件: 第一个工作区比较重要,go get会自动从一些主流公用代码仓库下载 ...
- 我的Java开发学习之旅------>使用Working Setst将Eclipse中的项目分类使项目一目了然
今天发现Eclipse中若有太多的项目,杂七杂八的,看起来会非常的痛苦.今天请教公司的前辈学会了一个方法,在Eclipse中,当项目比较多的时候,我们可以用WorkingSet将这些项目分类,把相关连 ...