【LeetCode】27 - Remove Element
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.
Solution 1:
int removeElement(vector<int>& nums, int val)
{
/*set a new indexto to record new length, if it is val then jump to next element and do nothing,
else, keep it. But there are some redundant assignment*/
if(nums.empty())return ;
int length=;
for(int i=;i<nums.size();i++)
{
if(nums[i]!=val)
nums[length++]=val;
}
return length;
}
Solution 2:
int removeElement(vector<int>& nums, int val)
{
/*the most beautiful solution, when it meets val, assignment this position with the last element, it cost less than once tranversal
Tips: the element from the last may be val, so i-- is necessary*/
if(nums.empty())return ;
int newlength=nums.size();
for(int i=;i<newlength;i++)
{
if(nums[i]==val)
{
nums[i]=nums[newlength-];
i--;
newlength--;
}
}
return newlength;
}
【LeetCode】27 - 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 解题报告(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 记录起始位置 日期 题目地址:https:/ ...
- 【一天一道LeetCode】#27. Remove Element
一天一道LeetCode系列 (一)题目 Given an array and a value, remove all instances of that value in place and ret ...
- 【LeetCode】027. Remove Element
题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...
- 【easy】27. Remove Element
删除等于n的数,并返回剩余元素个数 Given nums = [3,2,2,3], val = 3, Your function should return length = 2, with the ...
- 【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][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
- LeetCode OJ 27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length. D ...
随机推荐
- Docker基础技术:DeviceMapper
在上一篇介绍AUFS的文章中,大家可以看到,Docker的分层镜像是怎么通过UnionFS这种文件系统做到的,但是,因为Docker首选的AUFS并不在Linux的内核主干里,所以,对于非Ubuntu ...
- SPOJ 370 Ones and zeros BFS + 同余剪枝
题意:给一些n,求出最小的只包含0,1的n的倍数 设两数a, b满足: a < b 并且a % n = b % n. 如果 ( a * 10^x + c ) % n = z , 根据同余定理,( ...
- Data Base MongoDB 插入时间不正确的问题
关于mongodb插入时间不正确的问题 mongodb插入时间: 把本地时间转换为utc时间: 也就是比本地时间少8个小时: 读取的时候又会转换本地时间: 所有一般不需处理:
- Mysql数据库连接、查询、记录集操作代码
Mysql数据库链接代码 function dbConnect($hostname,$username,$pass,$db_name,$pconnect =0) { $func=empty($pcon ...
- 【Tech】YCSB-0.1.3安装使用
1. 下载YCSB 0.1.3: wget https://github.com/brianfrankcooper/YCSB/archive/0.1.3.tar.gz 如果提示“wget:命令没找到” ...
- Zend13.0 +XAMPP3.2.2 调试配置
Zend 调试PHP有3种方式: (1)PHP CLI APPLICATION (2)PHP Web Application (3)PHP UnitTest (1).(2)两种方式配置相似,下图是配置 ...
- 《OD大数据实战》Kafka入门实例
官网: 参考文档: Kafka入门经典教程 Kafka工作原理详解 一.安装zookeeper 1. 下载zookeeper-3.4.5-cdh5.3.6.tar.gz 下载地址为: http://a ...
- C++ new、delete
C++中向系统申请堆内存的方法为使用new.new[]操作符,new申请单个对象的内存,new[]申请对象数组的内存.对应的delete.delete[]操作符将new.new[]操作符申请到的内存还 ...
- config windows virtual machine on mac
1.download virtualbox and related extension pack from http://www.oracle.com/technetwork/server-stor ...
- Tomcat,Jboss,Glassfish等web容器比较选型
概述 Web容器是一种服务调用的规范,J2EE运用了大量的容器和组件技术来构建分层的企业应用.在J2EE规范中,相应的有WEB Container和EJB Container等. Web容器给处于其中 ...