Leetcode Remove duplicates from Sorted Array (C,c++,java,python)

Source: Internet
Author: User

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)

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.