80. Remove Duplicates from Sorted Array II java solutions

Follow up for "Remove Duplicates":
What if duplicates are allowed at most twice?

For example,
Given sorted array nums = [1,1,1,2,2,3],

Your function should return length = 5, with the first five elements of nums being 1122 and 3. It doesn't matter what you leave beyond the new length.

 

Subscribe to see which companies asked this question

 1 public class Solution {
 2     public int removeDuplicates(int[] nums) {
 3         if(nums.length < 3) return nums.length;
 4         int s = 1;
 5         for(int i = 2; i < nums.length; i++){
 6             if(nums[i] != nums[s-1]) nums[++s] = nums[i];
 7         }
 8         return s+1;
 9     }
10 }

 

posted @ 2016-07-06 14:54  Miller1991  阅读(114)  评论(0编辑  收藏  举报