1 class Solution 
 2 {
 3 public:
 4     int removeDuplicates(vector<int>& nums) 
 5     {
 6         int sz=nums.size();
 7         if(sz<3)
 8             return sz;
 9         int i=1,j=1,count=1; 
10         while(j<sz)
11         {
12             if(nums[j]==nums[j-1])
13             {
14                 count++;
15                 if(count>2)
16                     j++;
17                 else
18                     nums[i++]=nums[j++];
19             }
20             else
21             {
22                 count=1;
23                 nums[i++]=nums[j++];
24             }    
25         }
26         return i;
27     }
28 };

双指针向前压缩,问题不大

posted on 2018-07-26 10:16  高数考了59  阅读(112)  评论(0)    收藏  举报