这道题限制了时间复杂度不能超过O(n^2),空间复杂度不能超过O(1),且不能修改原数组,这样可以使用的方法就非常有限了。
解题思路和142. Linked List Cycle II一样使用快慢指针。
source code
1 | // Runtime: 72 ms, faster than 48.84% of JavaScript online submissions for Find the Duplicate Number. |
test cases
1 | test("test1", () => { |