Leetcode#26 Remove Duplicates from Sorted Array

原创 2015年07月06日 22:50:00

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;

    }


版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011472124/article/details/46780269

Remove Duplicates from Sorted Array II -- LeetCode

原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/  这道题跟Remove Duplica...
  • linhuanmars
  • linhuanmars
  • 2014-04-23 08:39:29
  • 11025

26. Remove Duplicates from Sorted Array (python)

Given a sorted array, remove the duplicates in place such that each element appear only once and ret...
  • qq_28119401
  • qq_28119401
  • 2016-10-30 15:58:23
  • 1315

Remove Duplicates from Sorted Array [python解]

题目要求:Givena sorted array, remove the duplicates in place such that each element appearonly once and ...
  • nadizhong4136
  • nadizhong4136
  • 2017-07-24 19:18:55
  • 402

LeetCode:Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in place such that each element appear only once and ret...
  • fytain
  • fytain
  • 2013-09-02 20:42:03
  • 2817

Remove Duplicates from Sorted Array -- LeetCode

原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/  这道题跟Remove Element类似,也是...
  • linhuanmars
  • linhuanmars
  • 2014-02-27 05:12:22
  • 11237

LeetCode 26 — Remove Duplicates from Sorted Array(C++ Java Python)

题目:http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array/ Given a sorted array, remo...
  • dragon_dream
  • dragon_dream
  • 2014-02-23 15:12:20
  • 4043

[leetcode-26]Remove Duplicates from Sorted Array(C)

问题描述: Given a sorted array, remove the duplicates in place such that each element appear only once ...
  • zdavb
  • zdavb
  • 2015-07-28 12:36:07
  • 319

LeetCode(80) Remove Duplicates From Sorted Array II

题目如下: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example,...
  • feliciafay
  • feliciafay
  • 2015-01-11 03:55:11
  • 1342

Leetcode_26_Remove Duplicates from Sorted Array

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41521063 Remove Duplicates fro...
  • pistolove
  • pistolove
  • 2014-11-27 23:21:40
  • 1255

[C++]LeetCode 26: Remove Duplicates from Sorted Array(有序数组去重)

Problem: Given a sorted array, remove the duplicates in place such that each element appear only onc...
  • emmaalways
  • emmaalways
  • 2015-05-02 21:43:54
  • 527
收藏助手
不良信息举报
您举报文章:Leetcode#26 Remove Duplicates from Sorted Array
举报原因:
原因补充:

(最多只允许输入30个字)