【LeetCode】27. Remove Element (2 solutions)
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.
解法一:
常规做法,设置一个新A数组的下标ind。
遍历过程中遇到elem就跳过,否则就赋给新A数组下标对应元素。缺点是有多余的赋值。
class Solution {
public:
int removeElement(int A[], int n, int elem) {
int ind = ;
for(int i = ; i < n; i ++)
{
if(A[i] != elem)
A[ind ++] = A[i];
}
return ind;
}
};

解法二:最优美的解法,当遍历过程中遇到elem,就用末尾的元素来填补。
这样甚至遍历不到一次。
注意:从末尾换过来的可能仍然是elem,因此i--,需要再次判断。
class Solution {
public:
int removeElement(int A[], int n, int elem)
{
int newlength = n;
for(int i = ; i < newlength; i ++)
{
if(A[i] == elem)
{
A[i] = A[newlength-];
i--;
newlength--;
}
}
return newlength;
}
};

【LeetCode】27. Remove Element (2 solutions)的更多相关文章
- 【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 解题报告(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 ...
随机推荐
- 编程之美读书笔记1.1——让CPU占用率曲线听你的指挥
http://blog.csdn.net/pipisorry/article/details/36189155 <strong><span style="font-size ...
- linked-list-random-node
https://leetcode.com/problems/linked-list-random-node/ // Using Reservoir sampling algorithm // http ...
- 刚開始学习的人非常有用:纯struts框架实现用户登陆
本人新手一枚.也在学习ssh.高手建议从struts2開始学起,所以我就先仅仅用struts2写了一个demo.能够有助于理解struts2在项目中的作用. 首先简单了解一下struts2 的MVC模 ...
- 基于Sql Server 2008的分布式数据库的实践(终结)
学习.操作心得 以前在做网站程序的时候一直用的是MYSQL,但是网上搜到MYSQL不支持分布式操作,然后便开始查询MSSQL的分布式数据库的设计与操作,后来在网上找到了<基于SQL SERVER ...
- Back Track 5 之 Web踩点 && 网络漏洞
Web踩点 CMS程序版本探测 Blindelephant 针对WORDPRESS程序的踩点工具,通过比较插件等一系列的指纹,判断版本. 格式: Python Blindelephant.py [参数 ...
- ubuntu安装ssh服务记录
执行命令: sudo apt-get install openssh-server 有时候会出现这个错误: unable to fetch some archives ,maybe run apt- ...
- google/protobuf/releases/tag/v3.4.0 下载
Protocol Buffers v3.4.0 Downloads 4.07 MB protobuf-cpp-3.4.0.tar.gz 5.02 MB protobuf-cpp-3.4.0.zip 4 ...
- [Algorithm] Radix Sort Algorithm
For example we have the array like this: [, , , , , ] First step is using Counting sort for last dig ...
- 多个桌面Deskspace如何使用
1 给Deskspace设置背景.在DeskSpace选项中设置显示背景为天空箱体图像(软件自带的图像效果,也可以使用静态图像,即自己的图片) 2 给六个桌面各设置一个背景(也可以使用同一个背景)右击 ...
- A Package Manager for Xcode -Xcode模板管理工具Alcatraz使用
一款功能强大的Xcode模板管理工具 一:安装之后的效果图 二:安装 1:先关闭 xcode 2: 打开 terminal 3: 复制粘贴运行如下代码 mkdir -p ~/Library/App ...