leetcode 27 Remove Element
2016-02-26 23:02
288 查看
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的数交换位置就好了。
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
题解:水题。遇到val就和最后一个不是val的数交换位置就好了。
class Solution { public: int removeElement(vector<int>& nums, int val) { int len=nums.size(); int e=len-1; for(int i=0;i<len;i++){ if(nums[i]==val){ while(nums[e]==val&&e>i){ e--; } if(e==i){ return i; } else{ swap(nums[e],nums[i]); } } } return len; } };
相关文章推荐
- 蓝桥杯:生命之树
- HBase的Shell操作
- 树莓派FM广播点歌系统
- 《python基础教程》第一章 基础知识
- HDU——2723Electronic Document Security(STL map嵌套set做法)
- 刚学C语言时理解性的写的2杈
- 软件公司的实习有感
- UIdynamic
- 查看思科路由器各种信息SHOW 命令总结----无私奉献版(转载请注明出处)
- sql查询重复记录、删除重复记录方法
- Jfinal配合Shiro进行权限控制
- C++: char* 与 string类型转换
- 《冬吴相对论•心时代文集之一:欢喜》读后感
- 原理分析之四:一次SQL查询的源码分析
- 2.4G无线通信模块NRF24L01在Cortex M上的使用
- Java 8 Lambda Expression 学习
- 面向对象程序设计第三次作业
- Apache+Tomcat集群配置+session共享
- Android中Context的总结及其用法
- I/O复用的高级应用二:聊天室程序