Remove Duplicates from Sorted Array
2015-07-15 16:48
295 查看
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 = [1,1,2], Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. 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 1 and 2 respectively. It doesn't matter what you leave beyond the new length.
代码:
int removeDuplicates(int* nums, int numsSize) { int pos = 0; int i; if(numsSize <= 1) return numsSize; for(i = 1; i < numsSize; i++) { if(nums[i] != nums[pos]) { nums[++pos] = nums[i]; } } return pos+1; }
相关文章推荐
- 获取字符串中最长的对称的字串的大小
- Java泛型,结合JSONObject动态生成指定的对象
- Vimdiff的用法
- eclipse Package Explorer视图设置过滤功能
- CentOS下配置HTTPS访问主机并绑定访问端口号
- 常见数据库加密技术对比
- CentOS下配置HTTPS访问主机并绑定访问端口号
- IOS开发系列--IOS程序开发概览(翻译自Kenshin Cui's Blog)
- Cantor的数表,注意一些题中数学的应用
- 添加php扩展 此博文包含图片 (2014-04-24 16:22:49)
- redis时间事件
- POJ3750
- Codeforces 558E A Simple Task(线段树区间更新)
- Spring Bean生命周期
- kbengine 0.6.5安装
- Dokuwiki
- MySQL视图学习整理
- ScrollView起始位置不是最顶部的解决办法
- PTAM算法流程介绍
- 【Android】检测是否处于Wifi环境,利用WebView实现浏览器app