LeetCode 27 Remove Element
Problem:
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.
Summary:
去掉数组中和val相等的值并返回最终得到的数组长度。
Solution:
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int len = nums.size();
int j = ;
for (int i = ; i < len; i++) {
if (nums[i] != val) {
nums[j++] = nums[i];
}
}
return j;
}
};
LeetCode 27 Remove Element的更多相关文章
- leetCode 27.Remove Element (删除元素) 解题思路和方法
Remove Element Given an array and a value, remove all instances of that value in place and return th ...
- 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 ...
- Leetcode 27 Remove Element STL
和remove zero类似的方法完成该题 class Solution { public: int removeElement(vector<int>& nums, int va ...
- Java [leetcode 27]Remove Element
题目描述: Given an array and a value, remove all instances of that value in place and return the new len ...
- 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 ...
- Leetcode 27. Remove Element(too easy)
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 ...
随机推荐
- Servlet容器Tomcat中web.xml中url-pattern的配置详解[附带源码分析]
目录 前言 现象 源码分析 实战例子 总结 参考资料 前言 今天研究了一下tomcat上web.xml配置文件中url-pattern的问题. 这个问题其实毕业前就困扰着我,当时忙于找工作. 找到工作 ...
- Spring+Quartz实现定时任务
MessageMgr.java package com.uyao.bid.common.message; import com.pominfo.framework.exception.PomInfoE ...
- c++的一些陷阱(1)
class String { public: String(]) { strcpy(p,pp); } ~String() { delete[] p; } char& operator[](in ...
- [转]Hibernate延迟加载与opensessioninviewFilter
原文地址:http://blog.csdn.net/a19881029/article/details/7916702 hibernate延迟加载: 一个person对应多个school,使用hibe ...
- JUnit备忘录
测试方法不应该有参数 使用junit做测试的时候发现总是报错:Method XXX should have no parameters; 后来发现是因为测试方法里面函数参数
- cogs 577 蝗灾 CDQ分治
第一道CDQ,抄了下helenkeller的代码,感觉和归并排序差不多... 因为左半边的修改肯定在右半边的询问之前,所以就不用管时间的限制了,可以直接x轴排序树状数组处理y轴... #include ...
- phpexcel导入数据提示失败
phpexcel导入excel时明明只有几行数据,却提示506行失败,原来是excel中有506行"无效数据"(看起来是空的,但是和没有数据不一样).
- 【Alpha版本】十天冲刺——日志集合贴
No Bug 031402401鲍亮 031402402曹鑫杰 031402403常松 031402412林淋 031402418汪培侨 031402426许秋鑫 Day1 Day2 Day3 Day ...
- 5 构建Mysql+heartbeat+DRBD+LVS集群应用系统系列之生产环境下drbd裂脑处理
preface 公司的业务变更,导致服务器要搬迁,所以需要关闭服务器,然后到新地在开启服务器. 关机前确定drbd+heartbeat+mysql是正常使用的,没有异常,Heartbeat和drbd都 ...
- Html中行内样式的设置
Html中行内样式的设置.. <html> <head> <title>显示的页面选项卡标题</title> <style type=" ...