27. Remove Element
2016-03-09 23:55
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.
题意:去除数组中的所有val值,返回数组的新长度。
思路:注意对题意的理解,题意说不关心超出长度之后的数值,则思路是把所val值依次往后置换,然后返回置换后的索引+1。
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
题意:去除数组中的所有val值,返回数组的新长度。
思路:注意对题意的理解,题意说不关心超出长度之后的数值,则思路是把所val值依次往后置换,然后返回置换后的索引+1。
class Solution { public: int removeElement(vector<int>& nums, int val) { int p, q; p = 0; q = nums.size() - 1; while (p <= q){ if (nums[p] == val){ nums[p] = nums[q]; nums[q] = val; q--; } else{ p++; } } return q + 1; } };
相关文章推荐
- 通用二进制安装mysql-5.7.11-linux-glibc2.5-x86_64
- git贡献代码流程
- 软件项目管理第一次作业
- 基于python3的杭电HDU ACM AC自动机
- linux中grub文件丢失或者错误解决方法
- 《Android开发艺术探索》读书笔记 (6) 第6章 Android的Drawable
- C语言学习心得一:__stdcall、__cdcel和__fastcall三者的区别
- servlet连接mysql
- date 日期类 框架存在
- 【hdu2546】饭卡——01背包
- Java 多态
- homework2
- 用OC翻译并且添加注解的一些官方Demo
- 助人为乐->递归与分治->双递归函数
- java你可能不知道的事(2)--堆和栈
- 31. Next Permutation
- 网站定时运行源码
- R语言:利用caret包中的dummyVars函数进行虚拟变量处理
- 理解thinkphp的命名空间
- android的上下文菜单和上下文操作模式