给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数。假设只有一个重复的整数,找出这个重复的数。
示例 1:
输入: [1,3,4,2,2]
输出: 2
示例 2:
输入: [3,1,3,4,2]
输出: 3
说明:
- 不能更改原数组(假设数组是只读的)。
- 只能使用额外的 O(1) 的空间。
- 时间复杂度小于 O(n2) 。
- 数组中只有一个重复的数字,但它可能不止重复出现一次。
普通方法,排序后查找,复杂度O(n^2logn)
class Solution {
public int findDuplicate(int[] nums) {
Arrays.sort(nums);
for(int i=0;i<nums.length-1;i++)
if(nums[i]==nums[i+1])
return nums[i];
return nums[nums.length-1];
}
}
beat100%
刚刚看完了循环链表,现在来解读下这题,nums[nums[fast]]这种值当索引的方式,就好像变成链表来查找,想象一下啊,他们的查找在跳跃有木有!!!结下来看我的另外一篇博客就可以了
leetcode 142. 环形链表 II(Linked List Cycle II)
class Solution {
public int findDuplicate(int[] nums) {
int fast = nums[nums[0]], slow = nums[0];
while(fast != slow){
fast = nums[nums[fast]];
slow = nums[slow];
}
slow = 0;
while(fast != slow){
slow = nums[slow];
fast = nums[fast];
}
return slow;
}
}
class Solution {
public int findDuplicate(int[] nums) {
// Find the intersection point of the two runners.
int tortoise = nums[0];
int hare = nums[0];
do {
tortoise = nums[tortoise];
hare = nums[nums[hare]];
} while (tortoise != hare);
// Find the "entrance" to the cycle.
int ptr1 = nums[0];
int ptr2 = tortoise;
while (ptr1 != ptr2) {
ptr1 = nums[ptr1];
ptr2 = nums[ptr2];
}
return ptr1;
}
}
446

被折叠的 条评论
为什么被折叠?



