您的位置:首页 > 其它

LeetCode: Remove Duplicates from Sorted Array

2014-09-25 08:43 190 查看

题目

https://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/

分析

从前向后遍历, 如果该元素不和前面元素重复, 就该元素排到前面。

代码

class Solution
{
public:
int removeDuplicates(int A[], int n)
{
if (n < 1)
return n;
int count = 1;
for (int i = 1; i < n; i++)
if (A[i] != A[i-1])
A[count++] = A[i];
return count;
}
};

参考

http://blog.csdn.net/xudli/article/details/8423225
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: