Given a sorted array, remove the duplicates in place such, all element appear only once and return the new L Ength.
Do the allocate extra space for another array, and you must does this on place with constant memory.
For example,
Given input array nums = [1,1,2]
,
Your function should return length = 2
, with the first of the elements of nums being and 1
2
Respectivel Y. It doesn ' t matter what are you leave beyond the new length.
Problem Solving Ideas:
See the example of the topic, feel very simple, do not return an int? Too simple, after the submission only found that the value of nums[] also have to change, but add line code on the line, Java implementation is as follows:
public int removeduplicates (int[] nums) { if (nums.length==0) return 0; int result=1,c=nums[0]; for (int i=0;i<nums.length;i++) { if (nums[i]>c) { c=nums[i]; result++; nums[result-1]=c; } } return result; }
Java for Leetcode 026 Remove duplicates from Sorted Array