LeetCode 26. Remove Duplicates from Sorted Array
2016-04-09 08:43
417 查看
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 =
nums being
// Two Pointers.
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
1and
2respectively. It doesn't matter what you leave beyond the new length.
// Two Pointers.
int removeDuplicates(vector<int>& nums) { if(nums.size() <= 1) return nums.size(); int i = 0; // first pointer int j = 1; // second pointer while(j < nums.size()) { if(nums[i] != nums[j]) { nums[++i] = nums[j++]; } else { j++; } } return i + 1; }
相关文章推荐
- Canvas的使用,放大缩小的自定义布局,不同的位置不同的点击事件
- 利用七牛存储7天远程自动备份LINUX服务器
- 电脑组装图文教程电子书
- memcached实战系列(二)memcached参数以及启动
- IDEA新功能之后缀代码自动补全
- memcached实战系列(二)memcached参数以及启动
- Linux kernel 内核学习路线
- 网络流DINIC模板
- 使用VNC连接管理VPS
- Java代码在实战中的优化
- JS学习笔记-事件绑定
- day23_文件上传与下载__JavaMail邮件开发
- poj 3280(简单区间dp)
- A simple REST framework on C/C++
- 共振峰manipulate笔记
- OData的初步认识
- hibernate的HelloWord
- grep、egrep、fgrep命令
- linux下安装boost与测试
- 操作系统添加账号并隐藏