leetCode #26 Remove Duplicates from Sorted Array

删除相同数字

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

 

posted @ 2017-11-14 23:23  小鸭酱的书签  阅读(97)  评论(0编辑  收藏  举报