码迷,mamicode.com
首页 > 其他好文 > 详细

Remove Duplicates from Sorted Array

时间:2015-04-01 11:12:38      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:

将一个排好序的数组中重复的数字删除

思路:维护头和尾指针,头指针始终指向最后一个不重复的数字,尾指针不断向数组尾部探索找到不重复的值之后将其拷贝到头指针

  1. class Solution {
  2. public:
  3. int removeDuplicates(int A[], int n) {
  4. if (n <= 1)
  5. {
  6. return n;
  7. }
  8. int count = 1, head = 0, tail = 1;
  9. while (tail < n)
  10. {
  11. while (A[tail] == A[head])
  12. tail++;
  13. if (tail < n)
  14. {
  15. A[++head] = A[tail++];
  16. count++;
  17. }
  18. }
  19. return count;
  20. }
  21. };




Remove Duplicates from Sorted Array

标签:

原文地址:http://www.cnblogs.com/flyjameschen/p/4382964.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!