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 ...
随机推荐
- 斑马打印机网卡ZebraNet配置(有线)
实图: 抽象图: 说明: 1.并口,用于连接斑马打印机一端 2.网络连接状态指示灯 3.打印状态指示灯 4.测试按钮,在连接打印机的情况下,按下此键,会打印出网卡信息. 5.网线接口 按下测试按钮之后 ...
- 注意,ruby循环体定义的变量在结束时后,变量还存在
a = [1, 2, 3] for i in a b = 123 p i end p "b:#{b}" p i <ruby语言编程> 129页 倒数 第8行
- [ActionScript] AS3代码实现曝光过度效果
package { import flash.display.Loader; import flash.display.SimpleButton; import flash.display.Sprit ...
- node在安装完成后,出现node不是内部或外部命令
node在安装完成后,查看node版本 node -v出现"node不是内部或外部命令"郁闷. 各种搜索之后,处理好了问题了. 一张图解决问题.
- [DataTable] datatable根据表中的字段进行排序
private DataTable SortTable(DataTable dt,string[] pids) { DataTable dt0 = dt.Clone(); //复制原表结构 ;i< ...
- maven Spring获取不到配置文件
如题: 如果在maven项目中,Spring获取不到配置文件, 把配置文件放到.src/main/resource文件夹下即可 import org.springframework.context.s ...
- sizeof usage & big / little endian
http://blog.csdn.net/w57w57w57/article/details/6626840 http://people.cs.umass.edu/~verts/cs32/endian ...
- Knockout
<button id="load">Load</button><ul data-bind="template: { foreach: ven ...
- python学习(二):python基本语法
前言:python基本的语法与其他语言诸如C,JAVA等类似,但个中有些许不同. 一.常规语法 1.变量名与关键字 与其他语言类似,变量名由字母.数字.下划线组成,且必须由字母开头. 变量使用不需要提 ...
- vc调用dll 示例
其实,调用dll文件的方法很多,不一定要使用LoadLibrary函数.如果使用的话,你就要预先声明dll中的函数,很麻烦. 下面是我使用dll时的一点技巧,就是引入lib文件,可以参考: 一.Win ...