Given a sorted array, remove the duplicates in place such, all element appear only once and return the new length. 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 B Eing 1 and 2 respectively. It doesn ' t matter what are you leave beyond the new length.
Test instructions: Deletes the repeating element from the sorted array and returns the new array length. No additional space is requested.
Int removeduplicates (int* nums, int numssize) { // int cand=nums[0]; // for (int i=1;i<numssize;i++) { // if (Cand==nums[i]) { // numsSize--; // }else{ // cand=nums[i]; // } // } int index =0; int j; for (j=1;j<numssize;j++) { if (Nums[index]!=nums[j]) { nums[++index]=nums[j]; } } return index+1;}
PS: Gee ... Again a double-pointer problem. Use two pointers, index and J, to point to the current element and the next element with a comparison, respectively.
Index initial is nums[0], here at the beginning I was wondering why not starting from 0, in fact, I think wrong ..... Slowly Enlightenment!!!
Leetcode 26. Remove duplicates from Sorted Array C language