LeetCode287. Find the Duplicate Number

这篇具有很好参考价值的文章主要介绍了LeetCode287. Find the Duplicate Number。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、题目

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

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

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

Example 1:

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

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

Constraints:

1 <= n <= 105
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.

Follow up:

How can we prove that at least one duplicate number must exist in nums?
Can you solve the problem in linear runtime complexity?文章来源地址https://www.toymoban.com/news/detail-806543.html

二、题解

class Solution {
public:
    int findDuplicate(vector<int>& nums) {
        if(nums.size() == 1) return -1;
        int slow = nums[0],fast = nums[nums[0]];
        while(fast != slow){
            slow = nums[slow];
            fast = nums[nums[fast]];
        }
        fast = 0;
        while(fast != slow){
            fast = nums[fast];
            slow = nums[slow];
        }
        return slow;
    }
};

到了这里,关于LeetCode287. Find the Duplicate Number的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode 1385. Find the Distance Value Between Two Arrays

    Given two integer arrays  arr1  and  arr2 , and the integer  d ,  return the distance value between the two arrays . The distance value is defined as the number of elements  arr1[i]  such that there is not any element  arr2[j]  where  |arr1[i]-arr2[j]| = d . Example 1: Example 2: Example 3: Constraints: 1 = arr1.length, arr2.length = 500 -1000 = ar

    2024年02月10日
    浏览(32)
  • Leetcode 3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K

    Leetcode 3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K 1. 解题思路 2. 代码实现 题目链接:3007. Maximum Number That Sum of the Prices Is Less Than or Equal to K 这一题我的思路上就是一个二分的思路,先确定一个上下界,然后不断通过二分来找到最大的price不超过k的值。 因此,剩下的

    2024年01月20日
    浏览(32)
  • 【LeetCode】287. 寻找重复数

    思路 要解决这道题首先要理解如何将输入的数组看作为链表。对于数组 nums 中的数字范围在 [1, n],考虑两种情况: 如果数组中没有重复的数字,以 [1, 3, 4, 2] 为例,将数组下标 n 和 nums[n] 建立映射关系f(n),即 n-f(n): 0-1, 1-3, 2-4, 3-2 ,从下标 0 出发, 根据 f(n) 计算出一个值,

    2024年02月14日
    浏览(27)
  • 【LeetCode】287.寻找重复数

    给定一个包含  n + 1  个整数的数组  nums  ,其数字都在  [1, n]  范围内(包括  1  和  n ),可知至少存在一个重复的整数。 假设  nums  只有  一个重复的整数  ,返回  这个重复的数  。 你设计的解决方案必须  不修改  数组  nums  且只用常量级  O(1)  的额外空间。

    2024年02月14日
    浏览(25)
  • LeetCode 1089. Duplicate Zeros

    Given a fixed-length integer array  arr , duplicate each occurrence of zero, shifting the remaining elements to the right. Note  that elements beyond the length of the original array are not written. Do the above modifications to the input array in place and do not return anything. Example 1: Example 2: Constraints: 1 = arr.length = 104 0 = arr[i] = 9 这题

    2024年02月11日
    浏览(25)
  • Leetcode 268. Missing Number

    Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array. Sum all the numbers as x x x and use n ( n + 1 ) 2 − x frac{n(n+1)}{2} - x 2 n ( n + 1 ) ​ − x .

    2024年02月14日
    浏览(34)
  • LeetCode447. Number of Boomerangs

    You are given n points in the plane that are all distinct, where points[i] = [xi, yi]. A boomerang is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k (the order of the tuple matters). Return the number of boomerangs. Example 1: Input: points = [[0,0],[1,0],[2,0]] Output: 2 Explanation: The two boomerangs

    2024年02月02日
    浏览(25)
  • leetcode - 260. Single Number III

    Given an integer array nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in any order. You must write an algorithm that runs in linear runtime complexity and uses only constant extra space. Example 1: Example 2: Example 3: Constraints: U

    2024年02月11日
    浏览(32)
  • LeetCode 933. Number of Recent Calls

    ou have a  RecentCounter  class which counts the number of recent requests within a certain time frame. Implement the  RecentCounter  class: RecentCounter()  Initializes the counter with zero recent requests. int ping(int t)  Adds a new request at time  t , where  t  represents some time in milliseconds, and returns the number of requests that has h

    2024年02月08日
    浏览(36)
  • LeetCode171. Excel Sheet Column Number

    Given a string columnTitle that represents the column title as appears in an Excel sheet, return its corresponding column number. For example: A - 1 B - 2 C - 3 … Z - 26 AA - 27 AB - 28 … Example 1: Input: columnTitle = “A” Output: 1 Example 2: Input: columnTitle = “AB” Output: 28 Example 3: Input: columnTitle = “ZY” Output: 701 Constraints:

    2024年02月19日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包