LeetCode题解——Remove Duplicates from Sorted Array
2015-07-29 10:45
330 查看
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array nums =
Your function should return length =
It doesn't matter what you leave beyond the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array nums =
[1,1,2],
Your function should return length =
2, with the first two elements of nums being
1and
2respectively.
It doesn't matter what you leave beyond the new length.
class Solution { public: int removeDuplicates(vector<int>& nums) { int length =0; int i=0, N = nums.size(); while(i<N) { while(i<N-1 && nums[i]==nums[i+1]) i++; nums[length] = nums[i]; length++; i++; } return length; } };
相关文章推荐
- Android Fragment 真正的完全解析(上)
- MySQL Study之--MySQL数据类型与Oracle数据类型对比
- spring+mybatis的优缺点
- Path Sum II
- 网页打不开问题之一的解决办法
- ThinkPHP函数详解:C方法
- SVN使用规范
- android-RelativeLayout实现水平布局
- Android 的适配问题
- SVN使用规范
- Android: Bluetooth Input 连接流程分析
- 第二章:图解摄像头分类与结构,原来好简单
- 二分查找算法
- php获取远程图片存储到本地
- 【C++】求数组中所有元素的排列或组合
- JSP入门
- 在Xcode中使用Alcatraz插件工具
- URAL 1934 spfa算法
- 友元函数的用途
- checkbox 选中自定义的个数