leetcode 27
27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
Example:
Given input array nums = [3,2,2,3], val = 3
Your function should return length = 2, with the first two elements of nums being 2.
去除数列中指定的数,返回剩余数的个数n,并将剩余的数存入原数列的前n位。
代码如下:
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
if(nums.empty())
{
return ;
}
int n = nums.size();
if(n == && nums[] == val)
{
return ;
}
if(n == )
{
return ;
}
int sum = ;
int j = ;
for(int i =; i < n; i++)
{
if(nums[i] != val)
{
nums[j] = nums[i];
j++;
sum++;
}
}
return sum;
}
};
哈哈, 又一次100%。

leetcode 27的更多相关文章
- 包、继承以及 LeetCode 27、28题
1 package.import 和 import static 1.1 Package Java 引入了包(Package)机制,提供了类的多层命名空间,用于解决类的命名冲突.类文件管理问题.Jav ...
- 前端与算法 leetcode 27.移除元素
目录 # 前端与算法 leetcode 27.移除元素 题目描述 概要 提示 解析 算法 @(目录) # 前端与算法 leetcode 27.移除元素 题目描述 27.移除元素 概要 题目本身其实挺简 ...
- LeetCode 27. Remove Element (移除元素)
Given an array and a value, remove all instances of that value in place and return the new length. D ...
- [LeetCode] 27. Remove Element 移除元素
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- Java实现 LeetCode 27 移除元素
27. 移除元素 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额 ...
- LeetCode 27 Remove Element (移除数组中指定元素)
题目链接: https://leetcode.com/problems/remove-element/?tab=Description Problem : 移除数组中给定target的元素,返回剩 ...
- LeetCode(27)题解:Remove Element
https://leetcode.com/problems/remove-element/ Given an array and a value, remove all instances of th ...
- LeetCode 27 Remove Element
Problem: Given an array and a value, remove all instances of that value in place and return the new ...
- [leetcode 27]Implement strStr()
1 题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ...
随机推荐
- PyDev-Python的Eclipse插件安装
PyDev官网:http://marketplace.eclipse.org/node/114 安装方法: 1,打开Eclipse,如果是初次使用,关闭欢迎页面,否则无法按照我说的方法安装. 2,打开 ...
- ICE中间件说明文档
ICE中间件说明文档 1 ICE中间件简介 2 平台核心功能 2.1 接口描述语言(Slice) 2.2 ICE运行时 2.2.1 ...
- 容易导致outofmemoryException内存泄漏异常的编码问题
1.System.Drawing方面的类使用问题 System.Drawing用到了很多系统的资源和非托管代码,所以使用的时候要特别小心,注意内存泄漏(Memory Leak) 2.new byte[ ...
- RSpec自定义matcher
链接 https://relishapp.com/rspec/rspec-expectations/v/3-4/docs/custom-matchers/define-a-custom-matcher ...
- 性能测试工具之Gatling
转载:http://ningandjiao.iteye.com/blog/2004579 Gatling一直是久闻其名但是未得机会运用,正好最近有需求做性能测试,于是趁此机会熟悉了一下,可以说,这是目 ...
- SQL Server 2005中的分区表(四):删除(合并)一个分区(转)
在前面我们介绍过如何创建和使用一个分区表,并举了一个例子,将不 同年份的数据放在不同的物理分区表里.具体的分区方式为: 第1个小表:2010-1-1以前的数据(不包含2010-1-1). 第2个小表: ...
- maven项目导入eclipse
maven项目的配置文件 web java文件都在src下面 src/main/java src/main/webapp src/main/webapp/web-inf 导入后要重新添加jar包,设置 ...
- [Java] Steam文件输入流
package test.stream; import java.io.FileInputStream; import java.io.FileNotFoundException; import ja ...
- jquery on off 方法
$("p").on("click",function(){alert("The paragraph was clicked.");}); $ ...
- go mobile 得生命周期事件
生命周期事件,就是状态从一个阶段切换成另外一个状态时触发的事件.所以我们可以看到 lifecycle.Event 的定义如下: 生命周期一共有下面四个阶段: lifecycle.StageDead ...