English 中文(简体)
为什么能“将阿雷拉的“数字”中的阵列“变成一个与圈子相连的名单上”?
原标题:Why can the array in [Find The Duplicate Number in Array] be "converted into a linkedlist with circle"?
  • 时间:2024-02-21 01:49:30
  •  标签:
  • algorithm

下面是一个问题:


Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.

只剩下一只南数,这又是重复的。

You must solve the problem without modifying the array nums and uses only constant extra space.

制约因素:

  • 1 <= n <= 10^5
  • nums.length == n + 1
  • 1 <= nums[i] <= n
  • All the integers in nums appear only once except for precisely one integer which appears two or more times.

实例:

  • Input: nums = [1,3,4,2,2]
  • Output: 2

在审议了这个问题之后,我想到了一个周期,但我确信,这必然会形成一个周期。

在阅读解决办法后,我发现Flinoyd的周期调查算法可以在此适用。

然而,我只是在寻找相关名单的周期时使用这一算法。 回答只是告诉我,我可以通过这样做来取得结果,但它却告诉我,我为什么可以这样做。 我总是对这个阵列如何能够“变成与圈相连接的一个名单”感到迷惑。 谁能帮助我证明这一点?

问题回答

The sum of n nonrepeative integers 1 <= k <= n is

<代码>Sn = n* (n + 1) / 2。

计算<代码>S,阵列序号总数

d = S - Sn is the duplicate number. Note that d should be less or equal to n.





相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签