leetcode_c++:Remove Element (027)
2016-05-13 00:08
435 查看
题目
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
算法
设置两个指针,一个为可以放位置的指针,一个是扫描指针复杂度
O(N)class Solution { public: int removeElement(vector<int>& nums, int val) { int ret = 0; for(int i = 0; i < nums.size(); ++i){ if(nums[i] != val) nums[ret++] = nums[i]; } return ret; } };
相关文章推荐
- const与define的区别!!!!!
- 设计模式(三) 工厂模式
- C#调用C++导出函数
- c语言学习笔记4之c语言的规则
- C++ MySQL存储二进制数据
- 值得推荐的C/C++框架和库
- 第7周 C语言程序设计(新2版) 练习1-19 颠倒字符串中的字符
- 【C++】:C++map的妙用之 计算字符串加和
- C++ hashmap——unordered_map
- c++ 表达式,值的种类
- 第7周 C语言程序设计(新2版) 练习1-18 删除空格制表符 (程序不能正确运行)
- C++空类的作用一
- 联发科Linkit 7688 (一) 上手及在Mac下搭建OpenWrt交叉编译环境,C语言编译Hello,World
- C++:[STL]浅谈Allocator以及详解STL之sequence container的操作及使用(vector)
- C语言学习新手常见问题
- Arithmetic Progressions
- C++笔记之关键字explicit
- C++笔记之关键字explicit
- 【C/C++】:如何获得一个float数的小数位数?
- C++中类和结构体的介绍