移除排序数组中重复元素
2016-08-10 03:57
369 查看
#include<iostream>
#include<vector>
using namespace std;
class Solution
{
public:
int removeDuplicates(vector<int>&nums)
{
if (nums.empty())
return 0;
int index = 0;
for (int i = 1; i < nums.size(); i++)
{
if (nums[index] != nums[i])
nums[++index] = nums[i];
}
return index + 1;
}
};
int main()
{
vector<int>nums = { 1, 1, 2, 3, 4, 4, 5, 6, 7 };
Solution c;
cout << "数组的长度:" << c.removeDuplicates(nums)<< endl;
system("pause");
return 0;
}
运行结果:
#include<vector>
using namespace std;
class Solution
{
public:
int removeDuplicates(vector<int>&nums)
{
if (nums.empty())
return 0;
int index = 0;
for (int i = 1; i < nums.size(); i++)
{
if (nums[index] != nums[i])
nums[++index] = nums[i];
}
return index + 1;
}
};
int main()
{
vector<int>nums = { 1, 1, 2, 3, 4, 4, 5, 6, 7 };
Solution c;
cout << "数组的长度:" << c.removeDuplicates(nums)<< endl;
system("pause");
return 0;
}
运行结果:
相关文章推荐
- emove Duplicates from Sorted Array——移除排序数组中重复元素
- LeetCode 26 Remove Duplicates from Sorted Array(从已排序数组中移除重复元素)
- Remove Duplicates from Sorted Array——移除排序数组中重复元素
- Leetcode 80. Remove Duplicates from Sorted Array II(从已排序数组中移除重复元素)
- LeetCode 26 Remove Duplicates from Sorted Array(从已排序数组中移除重复元素)
- Leetcode刷题记—— Remove Duplicates from Sorted Array II(已排序数组移除重复元素2)
- Lettcode_26_Remove Duplicates from Sorted Array——移除排序数组中重复元素
- LeetCode: Remove Duplicates from Sorted Array II(在排序数组中删除重复元素)
- JavaScript移除数组内重复元素的方法
- leetcode 题解:Remove Duplicates from Sorted Array II(已排序数组去三次及以上重复元素)
- 数组试题---一个已经排序的数组,去除里面重复的元素 .
- leetcode:Remove Duplicates from Sorted Array去掉排序数组中重复的元素
- 去除数组的重复元素并排序
- 2011-12-04 15:36 JAVA实现位向量给无重复元素的整数数组排序
- leetcode 4. 移除有序数组中的重复元素 Remove Duplicates from Sorted Array
- 计蒜客第11题---移除数组中的重复元素
- 移除数组中的重复元素。
- 删除数组中重复出现的元素,只留下一个,排序后输出
- 输入一个数组,对数组进行排序,并输出该数组中重复元素出现的次数
- 18 如何从一个数组中移除重复的元素