Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example,
Given input array A =[1, 1, 2]
,
Your function shocould return length =2
, And a is now[1, 2]
.
The solution is simple. If Boundary Condition A is null or the length is 0, 0 is returned. Otherwise, the variable size is used to maintain the current subscript of the array required by the question until different elements are found, assign a value to the next space indicated by size. The final response should be the length, I .e., the subscript of the final element + 1;
Public Class Solution { Public Int Removeduplicates ( Int [] ){ If (A = Null | A. Length = 0 ){ Return 0 ;} Int Size = 0 ; Int Len = A. length; For ( Int I = 0; I <Len; I ++ ){ If (A [I]! = A [size]) {[ ++ Size] = A [I] ;}} Return Size + 1 ;}}
Leetcode-remove duplicates from sorted Array