#26 Remove Duplicates from Sorted Array
2016-05-04 19:12
274 查看
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 =
Subscribe to see which companies asked this question
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.
Subscribe to see which companies asked this question
class Solution { public: int removeDuplicates(vector<int>& nums) { int l = nums.size(); if(l == 0) return 0; int index = 0; for(int i = 1; i < l; i++) { if(nums[i] != nums[index]) nums[++index] = nums[i]; } return index + 1; } };
相关文章推荐
- map2
- Cannot subclass final class class com.sun.proxy.$Proxy16
- 18年经验总结:互联网+创业启动不用CTO的秘密!
- 18年经验总结:互联网+创业启动不用CTO的秘密!
- iOS tableView 一共0行时候去掉分割线 和 去掉多余分割线
- iOS开发系列--通知与消息机制
- gerrit安装步骤及控制台输出
- openssl生成公钥私钥对 加解密
- HDU 1391 Number Steps(数学题+找规律)
- jquery easyui-datagrid 如何清空数据
- Swift 中 10 个震惊小伙伴的单行代码
- AYITACM2016省赛第三周 C-妥协(最长公共单词序列)
- iOS UIKit:viewController之Segues (4)
- OpenCV成长之路6:实现读入图片并且对图片进行复制
- 高仿今日头条(2)
- 高仿今日头条(2)
- maven copy
- Catalan数2
- JavaMail的java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream错误
- 高仿今日头条(2)