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

【leetcode】Remove Duplicates from Sorted Array

时间:2014-05-21 20:00:51      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   c   code   java   

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].


题解:简单题,用三个游标:next_position,single和current;

  1. current指向当前考虑的元素;
  2. next_position指向下一个新元素放置的位置;
  3. single指向新元素第一次出现的地方;

每次当current和single指向的元素相等时,就把current前移直到current和single不相等;把single指向的元素放到next_position那里,然后next_position++,继续循环。每次current前移的时候要把对应的数组长度减一。

代码如下:

bubuko.com,布布扣
class Solution {
public:
    int removeDuplicates(int A[], int n) {
        int next_position = 0;// next_postition指向第一个可以被覆盖的元素的前一个元素
        int current = 0;// current指向当前考虑的元素
        int single = 0;//在第一次遇见某个元素时,就用single指向它,然后移动current
        int len = n;

        while(current < n){
            single = current;
            current ++;
            if(current < n){
                while(current < n && A[current] == A[single]){//current和next_position指向的元素重复
                    current ++;
                    len--;
                }
                A[next_position] = A[single];
                next_position++;
            }
            else{
                A[next_position] = A[single];
                break;
            }
        }

        return len;
    }
};
bubuko.com,布布扣

 在网上搜索了一下,发现自己的代码太麻烦了,只要两个变量next_position和current就可以了:

bubuko.com,布布扣
 1 class Solution {
 2 public:
 3     int removeDuplicates(int A[], int n) {
 4         if(n == 0)
 5             return n;
 6         int next_position = 0;
 7         for(int current = 1; current < n;current ++){
 8             if(A[next_position] != A[current]){
 9                 A[++next_position] = A[current];
10             }
11         }
12 
13         return next_position+1;
14     }
15 };
bubuko.com,布布扣

还有大神用STL的unique和distance实现:

unique(这里只用到它的第一个版本,即用==判断相等的版本):

bubuko.com,布布扣
Defined in header <algorithm>
template< class ForwardIt > ForwardIt unique( ForwardIt first, ForwardIt last );

Removes all consecutive duplicate elements from the range [first, last) and returns a past-the-end iterator for the new logical end of the range.

bubuko.com,布布扣

从上面的解释可以看到unique主要用来去除连续的重复元素,所以在使用unique之前最好确保数组是有序的。它的返回值是去重后最后一个元素的位置。

distance:

bubuko.com,布布扣
Defined in header <iterator>

template< class InputIt >
typename std::iterator_traits<InputIt>::difference_type 
    distance( InputIt first, InputIt last );

Returns the number of elements between first and last.
The behavior is undefined if last is not reachable from first by (possibly repeatedly) incrementing first.
bubuko.com,布布扣

这个函数就是返回first和last之前的元素个数。

实现代码如下:

bubuko.com,布布扣
class Solution {
public:
    int removeDuplicates(int A[], int n) {
        return distance(A,unique(A,A+n));
    }
};
bubuko.com,布布扣

以后就算简单题也要搜搜看别人怎么做的,自己做过的题,再看别人的代码,这样才能进步,不会固步自封。

 

参考:http://zhangxc.com/2013/11/leetcode-remove-duplicates-from-sorted-array

 

 

 

【leetcode】Remove Duplicates from Sorted Array,布布扣,bubuko.com

【leetcode】Remove Duplicates from Sorted Array

标签:style   blog   class   c   code   java   

原文地址:http://www.cnblogs.com/sunshineatnoon/p/3739592.html

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