Remove Element
2015-08-02 15:59
375 查看
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.
此题与Remove Duplicates from Sorted Array类似的做法
class Solution {
public:
//与Remove Duplicates from Sorted Array类似的做法
int removeElement(vector<int>& nums, int val) {
int len = nums.size();
if(len < 1)
return 0;
if(len == 1)
{
if(nums[0] == val)
return 0;
return 1;
}
int low = 0, high = 0;
while(high < len)
{
if(nums[high] != val)
{
nums[low++] = nums[high];
}
++high;
}
return low;
}
};
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
此题与Remove Duplicates from Sorted Array类似的做法
class Solution {
public:
//与Remove Duplicates from Sorted Array类似的做法
int removeElement(vector<int>& nums, int val) {
int len = nums.size();
if(len < 1)
return 0;
if(len == 1)
{
if(nums[0] == val)
return 0;
return 1;
}
int low = 0, high = 0;
while(high < len)
{
if(nums[high] != val)
{
nums[low++] = nums[high];
}
++high;
}
return low;
}
};
相关文章推荐
- prototype Element学习笔记(篇一)
- prototype Element学习笔记(Element篇三)
- prototype Element学习笔记(篇二)
- 块元素block element和内联元素inline element
- 分离式javascript取当前element值的代码
- javascript add event remove event
- Jquery替换已存在于element上的event的方法
- 关于jQuery object and DOM element
- IE不支持option的display样式,只能使用remove和add
- angular.element方法汇总
- jquery使用remove()方法删除指定class子元素
- Lua的table库函数insert、remove、concat、sort详细介绍
- MongoDB Remove函数的3个常见用法
- jquery之empty()与remove()区别说明
- ASP.NET之Response.Cookies.Remove 无法删除COOKIE的原因
- Datatable删除行的Delete和Remove方法的区别介绍
- jQuery中remove()方法用法实例
- 1. 一维数组
- jQuery方法区别(七)detach() remove()区别
- jQuery方法区别(七)detach() remove()区别