Problem:
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 and elements Of nums Being 1
and 2
respectively. It doesn ' t matter what are you leave beyond the new length.
Solution: Because it is an ordered array, when the first element and the i-1 element are equal, the I element must be a repeating element, with two cursors, one on the left, a non-repeating array, one on the right, and the original array, when there is no repetition of the elements, Assign this element to the top of a non-repeating array and move the Zoo right One
Title: To a well-ordered array, requires that all duplicate elements be removed, and no additional storage space can be used.
Java source Code (337MS):
public class Solution {public int removeduplicates (int[] nums) { int size=0,len=nums.length; for (int i=0;i<len;i++) { if (i==0 | | nums[i]!=nums[i-1]) { nums[size++]=nums[i]; } } return size; }}
C Language Source code (30MS):
int RemoveDuplicates (int* nums, int numssize) { int i,size=0; for (i=0;i<numssize;i++) { if (!) ( I>0 && nums[i]==nums[i-1]) { nums[size]=nums[i]; size++; } } return size;}
C + + source code (38MS):
Class Solution {public: int removeduplicates (vector<int>& nums) { int size=0,len=nums.size (); for (int i=0;i<len;i++) { if (i==0 | | nums[i]!=nums[i-1]) { nums[size]=nums[i]; size++; } } return size; };
Python source code (94MS):
Class solution: # @param {integer[]} nums # @return {integer} def removeduplicates (self, nums): size=0 ; Length=len (nums) for i in range (length): if i==0 or nums[i]!=nums[i-1]:nums[size]=nums[i];size+=1 return size
Leetcode Remove duplicates from Sorted Array (C,c++,java,python)