首页 > 其他 > 详细

Leetcode#80 Remove Duplicates from Sorted Array II

时间:2015-01-28 17:37:03      阅读:265      评论:0      收藏:0      [点我收藏+]

原题地址

 

简单模拟题。

从先向后遍历,如果重复出现2次以上,就不移动,否则移动到前面去

 

代码:

 1 int removeDuplicates(int A[], int n) {
 2         if (n == 0) return n;
 3         
 4         int len = 1;
 5         int dupSum = 1;
 6         
 7         for (int i = 1; i < n; i++) {
 8             if (A[i] == A[i - 1]) {
 9                 dupSum++;
10             }
11             else
12                 dupSum = 1;
13             if (dupSum <= 2) {
14                 A[len] = A[i];
15                 len++;
16             }
17         }
18         
19         return len;
20 }

 

Leetcode#80 Remove Duplicates from Sorted Array II

原文:http://www.cnblogs.com/boring09/p/4256361.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!