码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 题解:Remove Duplicates from Sorted Array(已排序数组去重)

时间:2014-07-03 20:58:08      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   strong   cti   for   

题目:

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 A = [1,1,2],

Your function should return length = 2, and A is now [1,2].

说明:

      1)无

实现:

 1 class Solution {
 2 public:
 3     int removeDuplicates(int A[], int n) {
 4         if(n==0)
 5         return 0;
 6         int B[n],k=0;
 7         for(int i=0;i<n;i++)
 8            B[i]=0;
 9            B[0]=A[0];
10         for(int i=1;i<n;i++)
11            {
12                if(B[k]!=A[i])
13                   B[++k]=A[i];
14            }
15            for(int i=0;i<=k;i++)
16                A[i]=B[i];
17            return k+1;
18         
19     }
20 };

 

leetcode 题解:Remove Duplicates from Sorted Array(已排序数组去重),布布扣,bubuko.com

leetcode 题解:Remove Duplicates from Sorted Array(已排序数组去重)

标签:style   blog   color   strong   cti   for   

原文地址:http://www.cnblogs.com/zhoutaotao/p/3821123.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!