leetcode第25题--Remove Element
problem:
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.
将一个数组里的和给定元素相同的元素删除,在该数组里操作,返回剩下的个数。注意,数组的前部分要保留答案。我本来直接扫一遍,输出n--,错了。还要改变A的值。如下
class Solution {
public:
int removeElement(int A[], int n, int elem) {
int result = n, j = ;
for (int i = ; i< n; i++)
{
if(A[i] == elem)
result--;
else
A[j++] = A[i];
}
return result;
}
};
leetcode第25题--Remove Element的更多相关文章
- 【LeetCode算法-27】Remove Element
LeetCode第27题 Given an array nums and a value val, remove all instances of that value in-place and re ...
- Leetcode 题目整理-7 Remove Element & Implement strStr()
27. Remove Element Given an array and a value, remove all instances of that value in place and retur ...
- 【算法】LeetCode算法题-Remove Element
这是悦乐书的第150次更新,第152篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第9题(顺位题号是27).给定整数数组nums和值val,删除nums中所有的val值, ...
- [算法题] Remove Element
题目内容 本题来源:LeetCode Given an array and a value, remove all instances of that value in place and retur ...
- leetcode第26题--Remove Duplicates from Sorted Array
problem: Given a sorted array, remove the duplicates in place such that each element appear only onc ...
- LeetCode记录之27——Remove Element
这道题跟26题很类似,并且有官方的答案.看了官方的答案之后发现写得特别巧,自己做的题太少思路太窄.有意思的是我的算法的时间复杂度是O(N^2),官方的是O(N),我的实际运行时间还少了2ms. ive ...
- leetcode第19题--Remove Nth Node From End of List
Problem: Given a linked list, remove the nth node from the end of list and return its head. For exam ...
- [LeetCode&Python] Problem 27. Remove Element
Given an array nums and a value val, remove all instances of that value in-placeand return the new l ...
- LeetCode(27)Remove Element
题目 Given an array and a value, remove all instances of that value in place and return the new length ...
随机推荐
- Python标准库09 当前进程信息 (部分os包)
原文:Python标准库09 当前进程信息 (部分os包) 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 我们在Linux的概念 ...
- Mac下Jekyll安装
之前一直用Wordpress,虽然功能强大,各种插件各种bug,如果想弄个主题,折腾得要命.最近改用jekyll+gitHub免费空间.记录一下. 我用的是Mac,所以只讲述Mac下如何安装,Wind ...
- java处理Excel文件---excel文件的创建,删除,写入,读取
这篇文章的代码是我封装的excel处理类,包含推断excel是否存在,表格索引是否存在,创建excel文件,删除excel文件,往excel中写入信息,从excel中读取数据. 尤其在写入与读取两个方 ...
- ContentProvider总结(Android)
ContentProvider 1.适用场景 1) ContentProvider为存储和读取数据提供了统一的接口 2) 使用ContentProvider,应用程序能够实现数据共享 3) andro ...
- .net卸载程序制作
原文:.net卸载程序制作 方法一: 在打包项目中添加文件msiexec.exe(一般在c:\windows\system32(系统目录中)找到). 在文件系统视图中选择应用程序文件,在msiexec ...
- MVC — 第 6 天
7 天玩转 ASP.NET MVC — 第 6 天 目录 第 1 天 第 2 天 第 3 天 第 4 天 第 5 天 第 6 天 第 7 天 0. 前言 欢迎来到第六天的 MVC 系列学习中.希望 ...
- Python标准库:内置函数format(value[, format_spec])
的值的函数value按format_spec的格式来格式化,然而函数解释format_spec是依据value的类型来决定的.不同的类型有不同的格式化解释. 当參数format_spec为空时,本函数 ...
- 工作介绍xml书包文件
光开放平台一个非常重要的特点就是简化了对xml文件的操作,您能非常轻松地引入xml文件.定位到随意节点.增删属性和文本以及节点本身,以下咱们用实例来介绍对xml的操作 引入xml文件: <cht ...
- CSM认证培训知识汇总
后两天的CSM训练.我们非常颠覆旧观念,有关的一些观点的简要培训记录: 约定大于监管:监督是管理水平.或旧有格局,该公约是一个团队与自己的法规. 工具:传统的管理工具和服务经理,还有许多人是PM正在使 ...
- Node.js v0.10.31API手工-DNS
原版的API品种,这是从以前的翻译和翻译风格不同 Node.js v0.10.31API手冊-文件夹 DNS 使用 require('dns') 引入此模块. dns 模块中的全部方法都使用了 C-A ...