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 being 1 and 2 respectively. It doesn ' t matter what are you leave beyond the new length.
Hide Tags:array, pointers
Topic 26: Enter an array that has been sequenced, in which there may be duplicate data, find duplicate data, bolt out redundant data, and keep only one duplicate data.
Idea: Record the number of repeated data, such as three of them are 5, then the number of repetitions is 2. The subsequent data is populated sequentially to the repeating data location.
if(nums[i] == nums[i+1count++;else nums[i+1-count] = nums[i+1
Topic 27: The subject and title 26 similar, 27 requirements will be linked with the given data Val repeating data are tied out, do not retain any one duplicate data, similar method, record repeats more than 26 questions one more line
ifcount++;else nums[i-count
#include <stdlib.h>#include <stdio.h>//26:remove duplicates from Sorted Array/ * Given A sorted array, remove the duplicates in place such, all element appear only once and return the new Le Ngth. * Do not 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 1 and 2 respectively. It doesn ' t matter what are you leave beyond the new length. *///ideas always accumulate the number of repetitions, array i bit = i-Number of repetitionsintRemoveDuplicates (int* Nums,intNumssize) {inti =0;int Count=0; for(i=0; i<numssize-1; i++) {if(Nums[i] = = nums[i+1]) {Count++; }Else{nums[i+1-Count] = nums[i+1]; } }returnnumssize-Count;}//27:remove Element//given An array and a value, remove all instances of that value in place and return the new length.//the Order of elements can be changed. It doesn ' t matter what are you leave beyond the new length.intRemoveelement (int* Nums,intNumssize,intVal) {inti =0;int Count=0; for(i=0; i<numssize;i++) {if(Nums[i] = = val) {Count++; }Else{nums[i-Count] = Nums[i]; } }returnnumssize-Count;}intMain () {intn[Ten] = {1,2,3,3,3,4,5,5,6,7};ints[Ten] = {1,2,3,4,5,6,7,8,9,Ten};intR = RemoveDuplicates (s,Ten); printf"R =%d\n", R);inti =0; for(i=0; i<r;i++) {printf ("n[%d] =%d\n", I,n[i]); }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Leetcode]-remove duplicates from Sorted Array