Leetcode 26. Remove duplicates from Sorted Array C language

Source: Internet
Author: User
Tags array length

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.