[Leetcode] 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.
题意:删除给定的数,然后返回新的长度。
思路:这题的思路和sort colors差不多,是其简化版。大致的思路是:使用两个指针,指针l 指前,指针 r 指后,遍历数组,遇到给定的数,则将指针 l 指向的元素和r指向的元素交换,每交换一次r--一次,然后重新从指针l处重新遍历;若不是给定的数,则直接跳过即可。代码如下:
class Solution {
public:
int removeElement(int A[], int n, int elem)
{
int count=;
if(n<) return ;
int l=,r=n-;
while(l<=r)
{
if(A[l]==elem)
{
swap(A[l],A[r])
r--;
count++;
}
else
l++;
}
return n-count;
}
};
思路二:从前向后遍历数组,遇到给定数,记下其位置,将其和后面第一个不为给定数交换,即可。代码如下:
class Solution
{
public:
int removeElement(int A[],int n,int elem)
{
int count=;
for(int i=;i<n;++i)
{
if(A[i]==elem)
count++;
else if(count>) //避免多个连续
A[i-count]=A[i];
}
return n-count;
}
}
[Leetcode] remove element 删除元素的更多相关文章
- LeetCode Remove Element删除元素
class Solution { public: int removeElement(int A[], int n, int elem) { ]; int i,num=n; ;i<n;i++){ ...
- leetCode 27.Remove Element (删除元素) 解题思路和方法
Remove Element Given an array and a value, remove all instances of that value in place and return th ...
- lintcode:Remove Element 删除元素
题目: 删除元素 给定一个数组和一个值,在原地删除与值相同的数字,返回新数组的长度. 元素的顺序可以改变,并且对新的数组不会有影响. 样例 给出一个数组 [0,4,4,0,0,2,4,4],和值 4 ...
- [leetcode]27. Remove Element删除元素
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- 在Python的列表中利用remove()方法删除元素的教程
在Python的列表中利用remove()方法删除元素的教程 这篇文章主要介绍了在Python的列表中利用remove()方法删除元素的教程,是Python入门中的基础知识,注意其和pop()方法的区 ...
- [LeetCode] Remove Element 分析
Remove Element算是LeetCode的一道水题,不过这题也有多种做法,现就我所知的几种做一点讨论. 题目链接:https://leetcode.com/problems/remove-el ...
- [LeetCode] Remove Element题解
Remove Element: Given an array and a value, remove all instances of that value in place and return t ...
- [LeetCode] Remove Element 移除元素
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- [LeetCode] Remove Element (三种解法)
Given an array and a value, remove all instances of that value in place and return the new length. T ...
随机推荐
- mysql在cmd里中文乱码解决办法
右边画红线部分中文已经乱码,左边红线里中文则完美显示出来了. 解决办法 用set names utf-8: 效果如图
- Scrapy框架的初步使用
Scrapy scrapy框架是一个非常全面的爬虫框架,可以说是爬虫界的django了,里面有相当多的组件,格式化组件item,持久化组件pipeline,爬虫组件spider 首先我们要先和djan ...
- ctf题目writeup(4)
2019.1.31 题目:这次都是web的了...(自己只略接触隐写杂项web这些简单的东西...) 题目地址:https://www.ichunqiu.com/battalion 1. 打开链接: ...
- My First Marathon【我的第一次马拉松】
My First Marathon A month before my first matathon, one of my ankles was injured and this meant not ...
- IO复用——poll系统调用
1.poll函数 #include<poll.h> int poll(struct pollfd* fds, nfds_t ndfs, int timeout) poll函数在一定的时间内 ...
- python2.7入门---文件I/O&简单用户交互
这篇文章开始之前,我们先来看下python中的输出方法.最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你传递的表达式转换成一个字符串表达式,并将结果写 ...
- flask与javascript及ajax
flask与javascript及ajax 1. flask+js 1.1. 最简单的 最简单的元素信息改变. {% block content %} <h1>我的第一张网 ...
- Spring + MySQL + Mybatis + Redis【二级缓存】
一.Redis环境 Redis 官网 :http://redis.io/ windows下载:https://github.com/dmajkic/redis/downloads 1.文件解压缩 2. ...
- .net core 新建一个web api 的步骤 初级
1.使用VS2017 选择 .net core web应用程序. 2.选择web api(空). 3.如果需要用iis express调试,则需要修改 program.cs. 4.在Controlle ...
- es同步mysql同步-logstash
1.下载es https://www.elastic.co/downloads/elasticsearch 修改 config 下elasticsearch.yml ip和端口等配置 2.下载ki ...