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.
题目的要求大概是给定一个数组和一个值,删除数组中所有和该值相同的元素并返回一个新的长度
说一下思路:
可以采用双指针,下标i循环数组,每次都让p_last下标与i一起遍历,当A[i]与给定的value相同的时候,p_last停止,数组长度-1,i继续走将下一个值赋给A[p_last],如果是A[i]与value不同的话,就让p_last+1
具体的代码如下:
class Solution {
public:
int removeElement(int A[], int n, int elem) {
int length = n;
int p_last = ;
for(int i=;i<n;i++){
A[p_last] = A[i];
if(A[i] == elem)
length--;
else
p_last++;
}
return length;
}
};
或者可以有一个更加直观的理解:
如下面的代码:
class Solution {
public:
int removeElement(int A[], int n, int elem) {
int length = n;
int p_last = ;
for(int i=;i<n;i++){
if(A[i]==elem){
length--;
}else{
A[p_last] = A[i];
p_last++;
}
}
return length;
}
};
p_last代表当前”新“数组的下标,i循环数组当A[i]!=elem时进行赋值,并将p_last加一”新“数组的下一个元素等待被插入个人认为这种方式解释方式比较直观。
27 Remove Element的更多相关文章
- [Leetcode][Python]27: Remove Element
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 27: Remove Elementhttps://oj.leetcode.c ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 27. Remove Element【easy】
27. Remove Element[easy] Given an array and a value, remove all instances of that value in place and ...
- leetCode练题——27. Remove Element
1.题目 27. Remove Element——Easy Given an array nums and a value val, remove all instances of that valu ...
- C# 写 LeetCode easy #27 Remove Element
27. Remove Element Given an array nums and a value val, remove all instances of that value in-place ...
- 【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 ...
- 27. Remove Element@python
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- leetCode 27.Remove Element (删除元素) 解题思路和方法
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 (移除元素)
Given an array and a value, remove all instances of that value in place and return the new length. D ...
随机推荐
- Unhandled event loop exception 解决办法
网上搜索了一下.对其他人有效的办法有两种: 1. 安装了adsafe .卸载掉就可以了. 2.安装了流氓软件:百度杀毒. 卸载掉也解决问题了 我就是第三种情况.到现在还没解决的热.!. 谁还有更好的 ...
- Ribbon2: 创建动态的Ribbon库
Sam Radakovitz曾在Excel团队博客中发表过一篇文章,介绍了如何创建动态的Ribbon库,即如何通过RibbonX和VBA放置动态的图形图像到功能区库中,在该文中,作者创建了两个库:一个 ...
- EntityFramework sum嵌套
一个查询中 用到了 sum , 可是返回结果的小数有很多位 , 都不准确了..类似js中的小数运算一样...不太熟悉C#,不知道这问题是因为double的关系 , 还是因为代码写的问题 , 通过 sq ...
- 《C++ Primer Plus 6th》读书笔记 - 第十一章 使用类
1. 运算符重载 2. 计算时间:一个运算符重载示例 3. 友元 1. 友元有三种: 友元函数 友元类 友元成员函数 4. 重载运算符:作为成员函数还是非成员函数 5. 再谈重载:一个矢量类 6. 类 ...
- sqlplus 链接数据库
实验目的:在虚拟机中用sqlplus工具访问真实机的数据库: 实验环境: 真实机(windows系统,数据库服务名 orcl): SQL> select * from v$version; BA ...
- 用纯CSS3绘制萌系漫画人物动态头像
大家已经见惯了用CSS3画的图标.LOGO.头像,这次台湾同学Rei给我们带来了用纯CSS3绘制的日本动漫<轻音少女>女主角秋山澪的动态头像.看到动图我震惊了!!!CSS3的强大再次霸气测 ...
- ajax全局函数运用
- 不同分辨率下获取不同js文件
获取当前网站的目录 //js获取网站根路径(站点及虚拟目录),获得网站的根目录或虚拟目录的根地址 function getRootPath(){ //整个域名(如:http://vc3.cn/ind ...
- Python学习(二) 运行Python,编译Python
无论windos还是Linux只要安装了python,配置好了环境变量,则在命令行输入python这个命令的时候就会进入交互模式.在这个模式下可以进行一些简单的python代码编写.退出可以使用exi ...
- undefined reference to `_sbrk', `_write', `_lseek', `_read'
现象: 在用GCC编译嵌入式MCU程序时,由于使用了第三方的库,出现了类似undefined reference to `_sbrk', `_write', `_lseek', `_read'的连接错 ...