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 ...
随机推荐
- RTUILabel+正则表达式
RTLabel和RegexKitLite都要导入第三方库 使用Regexkitlite库进行正则表达式的解析 1.库是使用MRR,如果在ARC工程里面使用这个类,必须在project->buil ...
- windows8开发学习笔记
XAML行列定义 <Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> &l ...
- java中的xpath,读取xml文档。
1,入门 XPath即为XML路径语言(XML Path Language),它是一种用来确定XML文档中某部分位置的语言. XPath基于XML的树状结构,提供在数据结构树中找寻节点的能力.起初 X ...
- xss框架(二)基础框架实现
简述 自上一篇博客介绍浏览器通信以来已经过去将近两个月了,兜兜转转挖了不少坑,也走了很多弯路.期间研究saml2.0和单点登录等技术都最后无疾而终. 只有xss框架这部分坚持了下来,这个框架还有很多事 ...
- Thinking in Java——集合(Collection)
一.ArrayList的使用(略) 二.容器的基本概念 (一).Collection是集合类的基本接口 主要方法: public interface Collection<E>{ bool ...
- 自定义View 一 (继承VIew重写onDraw方法)
项目:具有圆形效果的自定义View 一.继承View并重写onDraw方法 public class CircleView extends View{ private static final int ...
- jquery ajax 跨域提交(附IE浏览器解决方案)
后台输出内容之前需要指定header("Access-Control-Allow-Origin: *"); post 之前 jQuery.support.cors = true; ...
- flask-sqlalchemy 关系表简单操作
from flask import Flaskfrom flask.ext.sqlalchemy import SQLAlchemyfrom flask.ext.migrate import Migr ...
- I/O事件
I/O事件 最近在研究tornado和gevent,里面涉及了非阻塞I/O.在了解非阻塞I/O之前,需要先了解I/O事件 我们知道,内核有缓冲区.假设有两个进程A,B,进程B想读进程A写入的东西(即进 ...
- GNU所有软件下载,其中最有意思的是octave
http://ftp.gnu.org/gnu/ 最有意思的是octave:https://www.gnu.org/software/octave/http://ftp.gnu.org/gnu/octa ...