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 1, 1, 2, 2 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 }

浙公网安备 33010602011771号