简体   繁体   English

是否可以使用 Floyd 的龟兔算法在 O(n) 时间、O(1) 空间复杂度内从未排序的数组中删除重复项?

[英]Is it possible to remove duplicates from an unsorted array in O(n) time, O(1) space complexity, using the Floyd's tortoise and hare algorithm?

Is it possible to remove duplicates from an unsorted array in O(n) time, O(1) space complexity, using the Floyd's tortoise and hare algorithm?是否可以使用 Floyd 的龟兔算法在 O(n) 时间、O(1) 空间复杂度内从未排序的数组中删除重复项? Consider the array [3,1,3,4,2].考虑数组 [3,1,3,4,2]。 After removing duplicates, the function "remove_dups" must return [3,1,4,2].删除重复项后,function“remove_dups”必须返回 [3,1,4,2]。 Also, the function should work on negative integers in the array.此外,function 应该适用于数组中的负整数。

Yes it is possible.对的,这是可能的。 The idea is to consider array items as linked list nodes.这个想法是将数组项视为链表节点。 Any particular index is pointing to the value at that index.任何特定索引都指向该索引处的值。

And you will see that there is loop in case of duplicate, two indexes will have same value, and they will form a cycle just like in the image given below.你会看到在重复的情况下存在循环,两个索引将具有相同的值,它们将形成一个循环,如下图所示。

Example:例子:

1->2->3->4->5->6->3 1->2->3->4->5->6->3

So we can find the entry point of cycle in the linked list and that will be our duplicate element.所以我们可以在链表中找到循环的入口点,这将是我们的重复元素。

Source: https://www.geeksforgeeks.org/find-duplicates-constant-array-elements-0-n-1-o1-space/资料来源: https://www.geeksforgeeks.org/find-duplicates-constant-array-elements-0-n-1-o1-space/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 搜索复杂度为O(log n)的算法,UNSORTED列表/数组 - Searching algorithm with complexity O(log n), UNSORTED list/array 如何使用时间复杂度&lt;O(n ^ 2)和空间复杂度O(n)快速按值对整数数组进行排序,然后按重复次数对整数数组进行排序 - How to sort array of integer first by value and second by number of repetition using swift in time complexity < O(n^2) and space complexity O(n) 数组子集问题[时间为O(n)的最小空间] - Array Subset problem [least space with O(n) time complexity] 在数组中查找不同值的O(1)空间复杂度的O(N ^ 2)时间复杂度 - Alternative O(N^2) time complexity with O(1) space complexity for finding distinct values in array 在数组中找到缺少的数字,时间复杂度O(N),空间复杂度O(1) - Find missing numbers in array, time complexity O(N), space complexity O(1) 确定大小为n的数组中的所有重复项,其中O(1)空间和O(n)时间内的值范围为0到n-2 - Determine all duplicates in array of size n having values in range 0 to n-2 in O(1) space and O(n) time 杂耍算法的时间复杂度应为O(n)吗? - Should time complexity of Juggling algorithm be O(n)? 未排序的数组 - 从下一个更高的值获取索引 | 复杂度 O(n),PHP - Unsorted array - Getting index from next higher value | Complexity O(n), PHP 用 O(n) 时间和 O(n) 空间比较两个数组的算法 - Algorithm with O(n) time and O(n) space to compare two arrays 是否可以在少于O(n)的时间内从排序列表中删除重复项? - Is it possible to remove duplicates from a sorted list in less than O(n) time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM