287. Find the Duplicate Number

本文介绍两种高效查找包含n+1个整数的数组中重复数字的方法:一种是利用链表思想定位重复元素;另一种采用二分查找策略逐步缩小范围确定重复项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem

Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one.

Note:

  1. You must not modify the array (assume the array is read only).
  2. You must use only constant, O(1) extra space.
  3. Your runtime complexity should be less than O(n2).
  4. There is only one duplicate number in the array, but it could be repeated more than once.

Solution

第一种解法,把它看成一个linked list , 当前index所对应的数值就是下一个要找位置的index。
简化成另外一个题目  142 Linked List Cycle II

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

第二种解法,用 binary search 的思想,每次去一半

  public static int findDuplicate(int[] nums) {
    if (nums.length == 0 || nums == null)
        return 0;
    int low = 1, high = nums.length - 1, mid;
    while (low < high) {
        mid = low + (high - low) / 2;
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] <= mid)
                count++;
        }
        if (count > mid)
            high = mid;
        else
            low = mid + 1;
    }
    return low;
}





标题基于Spring Boot的骑行路线规划与分享平台研究AI更换标题第1章引言介绍骑行路线规划与分享平台的研究背景、意义、国内外现状以及本论文的方法和创新点。1.1研究背景与意义分析骑行运动普及和路线分享需求,阐述平台设计的必要性。1.2国内外研究现状概述国内外在骑行路线规划与分享方面的技术发展和应用现状。1.3研究方法与创新点说明本文采用的研究方法和实现的创新功能。第2章相关理论与技术介绍Spring Boot框架、路线规划算法和分享技术的基础理论。2.1Spring Boot框架概述解释Spring Boot的核心概念和优势,以及在本平台中的应用。2.2路线规划算法原理阐述常用的路线规划算法,如Dijkstra、A等,并分析其适用场景。2.3分享技术实现方式介绍平台实现路线分享所采用的技术手段,如社交媒体集成、二维码生成等。第3章平台需求分析与设计详细阐述骑行路线规划与分享平台的需求分析、系统设计和数据库设计。3.1需求分析从用户角度出发,分析平台应具备的功能和性能要求。3.2系统设计设计平台的整体架构、模块划分以及各模块之间的交互方式。3.3数据库设计根据平台需求,设计合理的数据库表结构和数据存取方式。第4章平台实现与测试说明平台的开发环境、关键模块的实现过程,以及系统测试的方法与结果。4.1开发环境搭建介绍开发平台所需的软硬件环境及其配置方法。4.2关键模块实现详细描述路线规划、路线分享等核心功能的实现细节。4.3系统测试与性能评估对平台进行功能测试、性能测试,并分析结果以验证系统的稳定性和可靠性。第5章结论与展望总结本文的研究成果,指出不足之处,并展望未来的研究方向和改进措施。5.1研究结论概括性地阐述本文的主要研究内容和取得的成果。5.2未来工作展望针对当前研究的局限性,提出未来可能的改进方向和扩展功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值