Remove Duplicates from Sorted Array

LeetCode OJ


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.

题目解释

给出一个sorted array(意思是指已经排好序了?),处理后数组里每一个元素只能出现一次,返回处理后的数组长度。

不能使用额外的数组空间,只能用已经给出的确定的内存空间。

分析

因为不太懂sorted array具体指的什么,第一次做的时候以为数组是随机的,相同元素出现的位置是随机的,然后题目也没给出limit time,随手就写了一个O(n^3)

for(int i = 0; i < num; i++){
for(int j = i+1; j < num; j++){
if(array[i] == array[j]){
for(int k = j; k < num-1; k++)
array[k] = array[k+1];
num--;
j--;
}
}
}

自然是T了。然后就把sorted array当做已经排好序的数组,那就容易多了,算法也都是O(1),一看代码就明白,水题,直接上代码。

way1

if (nums.empty()) return 0;
int index = 0;
for (int i = 1; i < nums.size(); i++) {
if (nums[index] != nums[i])
nums[++index] = nums[i];
}
return index + 1;

way2 STL

return distance(nums.begin(), unique(nums.begin(), nums.end()));
  • std::distance

    template

    typename iterator_traits::difference_type

    distance (InputIterator first, InputIterator last);

    Return distance between iterators

    Calculates the number of elements between first and last.

    c++ reference

  • std::unique

    equality (1)

    template

    ForwardIterator unique (ForwardIterator first, ForwardIterator last);

    predicate (2)

    template <class ForwardIterator, class BinaryPredicate>

    ForwardIterator unique (ForwardIterator first, ForwardIterator last,

    BinaryPredicate pred);

    Remove consecutive duplicates in range

    Removes all but the first element from every consecutive group of equivalent elements in the range [first,last).

    c++ reference

相关题目

RemoveDuplicatesfromSortedArrayII

Remove Duplicates From Sorted Array的更多相关文章

  1. [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  2. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

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

  3. 【leetcode】Remove Duplicates from Sorted Array II

    Remove Duplicates from Sorted Array II Follow up for "Remove Duplicates":What if duplicate ...

  4. 26. Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  5. 50. Remove Duplicates from Sorted Array && Remove Duplicates from Sorted Array II && Remove Element

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  6. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

  7. 【26】Remove Duplicates from Sorted Array

    [26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...

  8. leetCode 26.Remove Duplicates from Sorted Array(删除数组反复点) 解题思路和方法

    Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such that e ...

  9. [Leetcode] Remove Duplicates From Sorted Array II (C++)

    题目: Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For ex ...

随机推荐

  1. jQuery立体式数字滚动条增加

    1.html结构 <div class="numberRun1"></div> 2.js <script type="text/javasc ...

  2. jQuery实现购物车多物品数量的加减+总价计算

        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3. ...

  3. SharePoint 2013 场解决方案包含第三方程序集

    前言 当我们使用SharePoint 场解决方案的时候,经常会包含第三方的程序集,而第三方的程序集经常会有强签名的问题,如果有强签名可以部署到GAC,没有的话也可以部署到应用程序下. 那么,很多初学者 ...

  4. 奇葩问题:This file could not be checked in because the original version of the file on the server was moved or deleted. A new version of this file has been saved to the server, but your check-in comments were not saved

    "This file could not be checked in because the original version of the file on the server was m ...

  5. Java中的查找算法之顺序查找(Sequential Search)

    Java中的查找算法之顺序查找(Sequential Search) 神话丿小王子的博客主页 a) 原理:顺序查找就是按顺序从头到尾依次往下查找,找到数据,则提前结束查找,找不到便一直查找下去,直到数 ...

  6. UI控件(复习一下)

    如何修改控件状态• 可见,确实需要经常修改控件状态• 那如何去修改控件的状态呢?方法很简单➢ 每一个UI控件都是一个对象➢ 修改UI控件的状态,其实就是修改控件对象的属性➢ 比如修改UILabel显示 ...

  7. linux下安装opcache扩展

    linux下安装opcache扩展   参考:http://www.php.net/manual/zh/opcache.installation.php 1 2 3 4 5 6 7 wget http ...

  8. CSS Sprites (CSS图像拼合技术)教程工具

    什么是CSS Sprites? “Sprite”(精灵)这个词在计算机图形学中有它独特的定义,由于游戏.视频等画质越来越高,必须有一种技术可以智能的处理材质和贴图,并且要 同时保持画面流畅.“Spri ...

  9. 定义返回Block的函数

    鉴于Block与函数的相似性,先从返回函数指针的函数入手 返回函数指针的函数 int fun1(int arg){ return arg + 1;}int fun2(int arg){ return ...

  10. Nginx 多站点配置

    最近学习和练习的时候,为Laravel应用程序添加了好几个站点,有些程序删除之后站点却还留着,这让强迫症感到非常难受,上次解决了这个问题之后并没有记录一下,于是导致今天又花了很多时间折腾,所以特地来写 ...