80. Remove Duplicates from Sorted Array II

就是比之前的多一个flag记录有没有重复过一次

 1     public int removeDuplicates(int[] nums) {
 2         if(nums == null || nums.length == 0) {
 3             return 0;
 4         }
 5         boolean flag = false;
 6         int index = 1;
 7         for(int i = 1; i < nums.length; i++) {
 8             if(nums[i-1] == nums[i]) {
 9                 if(flag == false) {
10                     flag = true;
11                     nums[index++] = nums[i];
12                 }
13             } else {
14                 nums[index++] = nums[i];
15                 flag = false;
16             }
17         }
18         return index;
19     }

 

posted @ 2016-04-03 03:29  warmland  阅读(115)  评论(0编辑  收藏  举报