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 ...
随机推荐
- 由WSDL文件生成WEB service server端C#程序(转)
一般一个已经实现功能的WEB Server会发布自己的WSDL文件,供客户端生成代理类. 但有时是先有的server与client交互的接口定义(WSDL)文件,然后由server和client端分别 ...
- function overloading/ declare function
Declare a function To declare a function without identifying the argument list, you can do it in thi ...
- 【iOS】objective-c 文档生成工具 appledoc
最近做ios framework的一些测试,提供给其他开发者使用的framework,API文档变得更加重要,以前没有接触过,这次尝试使用了一把appledoc来生成一下文档,感觉还不错. 首先,是从 ...
- JS数组删除一个元素(根据值删)
<script type="text/javascript"> <!-- // 删除数组中第一个匹配的元素,成功则返回位置索引,失败则返回 -1. Array.p ...
- WINFORM窗体里使用网页控件的一些办法
最近弄一个项目,是个CS的.当然也是有表单之类的,如果将HTML表单搬到窗体上就省事多了. .首先要使用一个控件 WebBrowser 载入页面没使用URL属性,使用了下面这个属性 this.webB ...
- SQL Server dbcc checkdb 修复
默认dbcc checkdb 只做数据库的检测数据库是否完好.不会主动做数据库的修复,要修复数据库,需要数据库设单用模式. 1.repair_allow_data_loss 可能导致数据丢失. 2. ...
- C#实现网页表单自动提交
首先,设计一个简单的Form界面,好直观的查看登录情况,界面如图下图所示: 然后在 webBrowser1_DocumentCompleted函数中添加如下代码: private void webBr ...
- VirtualBox 上安装CentOS 6.5
目标:1.在VirtualBox中安装CentOS2.配置虚拟机网络,实现: a.主机联网后,宿机能够通过主机上网 b.不管主机联网与否,主机都能SSH登录宿机,并且主宿机能互相传送文件 ...
- 使用AES加密的帮助类
在开发中经常使用加密/解密对一些内容进行处理,比如密码在存入数据库之前先经过加密处理等等,这里就把一个加密帮助类代码贴出来,供以后查找使用. 这个帮助类主要功能是对字符串和字节数组进行加密解密处理. ...
- progressbar样式
http://www.oschina.net/question/8676_11797 http://blog.csdn.net/ouyangtianhan/article/details/656576 ...