80. Remove Duplicates from Sorted Array II

 1 class Solution {
 2     public int removeDuplicates(int[] nums) {
 3         if(nums.length == 0) return 0;
 4         if(nums.length == 1) return 1;
 5         int i = 1, j = 1;
 6         int count = 1;
 7         while(j < nums.length) {
 8             if(nums[j] == nums[j-1] && count < 2) {
 9                 nums[i] = nums[j];  //这边也要换
10                 j++;
11                 i++;
12                 count++;
13             }else if(nums[j] == nums[j-1] && count >= 2) {
14                 j++;
15                 count++;
16             }else {
17                 nums[i] = nums[j];
18                 count = 1;
19                 j++;
20                 i++;
21             }
22             
23         }
24         return i;
25     }
26 }

 

posted @ 2018-09-15 22:59  jasoncool1  阅读(129)  评论(0编辑  收藏  举报