没有合适的资源?快使用搜索试试~ 我知道了~
LeetCode Remove Duplicates from Sorted Array解决方案
需积分: 0 0 下载量 91 浏览量
2024-01-23
23:15:57
上传
评论
收藏 11KB DOCX 举报
温馨提示
试读
1页
LeetCode Remove Duplicates from Sorted Array解决方案
资源推荐
资源详情
资源评论
Question:
Given a sorted array nums, 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 by modifying the input array
in-place with O(1) extra memory.
Example 1:
Given 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 returned length.
Example 2:
Given nums = [0,0,1,1,1,2,2,3,3,4],
Your function should return length = 5, with the first five elements of nums being modified to 0,
1, 2, 3, and 4 respectively.
It doesn't matter what values are set beyond the returned length.
Solution:
~~~
int removeDuplicates(vector<int>& nums) {
vector<int>::iterator iter = nums.begin();
while (nums.begin() != nums.end())
{
vector<int>::iterator temp = iter;
vector<int>::iterator temp2 = ++iter;
if (temp2 == nums.end()) break;
if (*temp == *temp2)
{
iter = nums.erase(temp);
}
}
return nums.size();
}
~~~
资源评论
句号(在IT行业摸爬滚打)
- 粉丝: 278
- 资源: 28
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功