【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 ...
随机推荐
- [Git] 谷歌的代码管理
copy from : http://www.ruanyifeng.com/blog/2016/07/google-monolithic-source-repository.html https:// ...
- Coursera课程python中的一些程序
Index of /code Name Last modified Size Description Parent Directory - BeautifulSoup.py 07-Aug-2015 1 ...
- html嵌套iframe怎样实现等iframe页面载入完进行下一步调用
</pre>假设想在你的html里面显示一张图片.或者显示一个报表,常常会在里面嵌套iframe,当我们点查询报表时.在报表显示过程中,我们想做个遮罩层,提示等待...可是报表显示出来后. ...
- @Java VisualVM分析堆转储文件
测试程序 public class HeapOOM { private static int i = 0; static class OOMObject { } public static void ...
- C语言文件打开方式及说明
ANSI C规定文件打开用函数fopen,关闭为fclose. 1.调用方式通常为: FILE *fp; fp=fopen(文件名, 打开方式); 2.参数说明: 文件名: 形如"myf ...
- go语言基础之切片和底层数组关系
1.切片和底层数组关系 示例: package main //必须有个main包 import "fmt" func main() { a := []int{0, 1, 2, 3, ...
- qt creator修改程序编码(解决中文乱码问题)的方法
qt creator修改程序编码(解决中文乱码问题)的方法 qt creator修改程序编码的功能有几处. 1.edit - select encoding 选择载入(显示)编码和储存编码,其中GB2 ...
- Cognos中新建SQLserver数据源的步骤
1:配置-数据源连接-新建数据源-指定数据源名称 2:选择数据库类型,暂时不配置jdbc 3:指定服务器,数据库名称,登陆用户名和密码 4:测试 5:测试OK(OLE DB类型的) 6:返回上一步 , ...
- JAVA 读取计算机中相关信息
java读取 计算机 cup号 读取版本号 显卡 .. . . ........ .. . . . package com.swt.common.util; import java.io.Buffer ...
- [Angular] Use ngx-build-plus to compile Angular Elements
We can treat Angular Element as each standlone lib and compile each Angular element spreatly. Tool w ...