LeetCode 287. Find the Duplicate Number

11 篇文章 0 订阅

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

There is only one duplicate number in nums, return this duplicate number.

Follow-ups:

How can we prove that at least one duplicate number must exist in nums?
Can you solve the problem without modifying the array nums?
Can you solve the problem using only constant, O(1) extra space?
Can you solve the problem with runtime complexity less than O(n2)?

Example 1:

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

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

Input: nums = [1,1] Output: 1 Example 4:

Input: nums = [1,1,2] Output: 1

Constraints:

2 <= n <= 3 * 104
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.

1. HashMap:
(用map储存num,然后找到个数大于1的数已经存在得数,复杂度o(n);
2. sort然后求重复的,复杂度O(nlogn)
3. Floyd Algs
写之前可以先做一下141. Linked List Cycle, 解法是相似的

Given head, the head of a linked list, determine if the linked list has a cycle in it. There is a cycle in a linked list if there is some
node in the list that can be reached again by continuously following
the next pointer. Internally, pos is used to denote the index of the
node that tail’s next pointer is connected to. Note that pos is not
passed as a parameter. Return true if there is a cycle in the linked
list. Otherwise, return false.
*

Example 1:

Input: head = [3,2,0,-4], pos = 1 Output: true Explanation: There is a
cycle in the linked list, where the tail connects to the 1st node
(0-indexed). Example 2:

Input: head = [1,2], pos = 0 Output: true Explanation: There is a
cycle in the linked list, where the tail connects to the 0th node.
Example 3:

Input: head = [1], pos = -1 Output: false Explanation: There is no
cycle in the linked list.

Constraints:
The number of the nodes in the list is in the range [0, 104].
-105 <= Node.val <= 105
pos is -1 or a valid index in the linked-list.

public class Solution {
    public boolean hasCycle(ListNode head) {
        if (head== null || head.next == null) return false;
        ListNode slow = head;
        ListNode fast = head.next;
        while (slow != fast) {
            if (fast == null || fast.next == null) {
                return false;
            }
            slow = slow.next;
            fast = fast.next.next;
        }
        return true;
    }
}

这是一个easy题,题目只要求得ListNode 是否循环, 并没有涉及pos的问题,看描述以为pos是一个参数,需要求出循环开始点是不是在pos位置,不过并非如此。因此只要设置一个兔子,一个乌龟,只要他俩能相遇,那么肯定是个环,比较简单。

回到287题, 对于数组,如果存在相同的数,我们同样可以把它之间的数以及这个数看成一个闭环,不同的是,我们需要找到闭环开始位置的数。关键就是兔子和乌龟怎么跑, 根据题目,数组长度是n+1, 数组元素范围是(0,n),每个元素对应的索引都在数组中存在值
在这里插入图片描述

  1. 对于上面数组,第一行是index;设置fast从6开始,slow从6开始
  2. slow路径:6->3->5->4->2->3->5…进入循环; fast路径:6->5->2->5->2…进入循环, 相遇点为2;(注意此时如果slow = nums[slow]了, 那么直接可以return了);
  3. 此时我们已经找到了相遇点了,为点2;

在这里插入图片描述
然后再设置一个乌龟, 让两个乌龟跑,他们相遇的地方就是所求点
Prof.
设起点到循环起点距离为x; 循环起点到相遇点距离为y; 相遇点到循环起点距离为z;

S兔 = x + y + z + x;
S龟 = x + y;
因为 S兔 = 2 * S龟;
因此 2x + 2y = 2x + y + z;
得出 x = z;

class Solution {
    public int findDuplicate(int[] nums) {
        if (nums.length == 0 || nums.length == 1) return 0;
        int slow = 0;
        int fast = 0;
        slow = nums[slow];
        fast = nums[nums[fast]];
        while (slow != fast) {
            slow = nums[slow];
            fast = nums[nums[fast]];
        }
        int slow2 = 0;
        while (slow2 != slow) {
            slow = nums[slow];
            slow2 = nums[slow2];
        }
        return slow;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值