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. 读《数据结构与算法 Javascript描述》 | 平淡无奇

    “平淡无奇”,一句话总结. 当初买这本书的原因,在意的是有没有什么令人惊喜的东西,特别是针对Javascript代码的奇思妙想,所以就买下了这本书. 在买的几本书里面,最先看的也是这一本,但看起目录就 ...

  2. 精通 CSS 选择器(二)

    补充了一些之前遗漏掉的选择器以及一些在 Selectors Level 4 中新定义的选择器. 属性选择器不区分大小写 [attribute="value" i],在 Select ...

  3. 解决windows防火墙无法启动的问题

    windows防火墙突然无法开启,找个各种方法,最后还是通过微软自动的修复工具修复的: 网址如下: https://support.microsoft.com/zh-cn/mats/windows_f ...

  4. Android NDK之JNI陷阱

    背景: 最近一个月一直在做移植库的工作,将c代码到share library移植到Android平台.这就涉及到Android NDK(native develop kit)内容.这里只想记录下JNI ...

  5. 多线程(pthread、NSThread、GCD)

    pthread C语言编写 跨平台可移植 线程生命周期需要我们来管理 使用困难 NSThread 面向对象的 可直接操作线程对象 线程生命周期需要我们来管理 使用简单 资源互斥(@synchroniz ...

  6. android 多线程下载 断点续传

    来源:网易云课堂Android极客班第八次作业练习 练习内容: 多线程 asyncTask handler 多线程下载的原理 首先获取到目标文件的大小,然后在磁盘上申请一块空间用于保存目标文件,接着把 ...

  7. nodejs API

    1.querystring参数处理 序列化 > querystring.stringify({'name':'scott',course:['jade','node'],from:''}) 'n ...

  8. 分组函数 ----group by 使用总结

    总结:1,在where子句中不能用分组聚合函数.       2,如果没有group by 子句,select 不能同时出现字段与分组的聚合函数.       3,在有 group by 的子句的查询 ...

  9. Flume应用场景及架构原理

    Flume概念 Flume是一个分布式.可靠.和高可用的海量日志聚合的系统,支持在系统中定制各类数据发送方,用于收集数据:同时,Flume提供对数据进行简单处理,并写到各种数据接受方(可定制)的能力. ...

  10. HTML基础(一)——一般标签、常用标签和表格

    第一部分  HTML <html>    --开始标签 <head> 网页上的控制信息 <title>页面标题</title> </head> ...