关闭

Leetcode#26 Remove Duplicates from Sorted Array

97人阅读 评论(0) 收藏 举报
分类:

Difficulty: Easy

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this in place with constant memory.

For example,
Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.

简单的题就用简单粗暴的方法。

int removeDuplicates(vector<int>& nums) {
        int len = nums.size();
        int a[len],cou=0;
        memset(a,0,sizeof(a));
        int i = 0;
        while(i<len){
            if(i>0&&nums[i]==nums[i-1]){
                a[i] = 1;
                cou++;}
            i++;
        }
        i=0;
        while(i<len)
        {
            if(a[i]==1)
                nums[i]=2147483647;
            i++;
        }
        sort(nums.begin(),nums.end());

        return len-cou;

    }


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:29806次
    • 积分:3265
    • 等级:
    • 排名:第10390名
    • 原创:311篇
    • 转载:4篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论