LeetCode-287 寻找重复数

传送门:https://leetcode-cn.com/problems/find-the-duplicate-number/submissions/
给定一个包含 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) 。
数组中只有一个重复的数字,但它可能不止重复出现一次。

我的实现方法之前写的博客也有,前提是数组中最大的 数要小于数组长度

int findDuplicate(int* nums, int numsSize) {
    int k, yl, sl;
    for (int n=0;n<numsSize;n++)
    {
        k = nums[n]&0xffff;
        yl = nums[k];//目标 yl为那个位置上的加密数据
        sl = yl >> 16;
        sl++;
        nums[k]=(sl<<16) |(yl&0xffff );
    }
    for (int g=0;g< numsSize;g++)
    {
        k = nums[g];
        yl = k >> 16;
        if (yl > 1)
        {
            printf("%d\n", g);
            return g;
        }
    }
    return 0;
}


速度还可以
就是修改了源数据 不知道在函数返回前再修改回去还可以不可以。

原文地址:http://blog.51cto.com/3458905/2323939

时间: 2024-08-01 06:16:21

LeetCode-287 寻找重复数的相关文章

leetcode 287. 寻找重复数(Find the Duplicate Number)

目录 题目描述: 示例 1: 示例 2: 解法: 题目描述: 给定一个包含 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) . 数组中只有一个重复的数字,但它可能不止重

LeetCode:寻找重复数【287】

LeetCode:寻找重复数[287] 题目描述 给定一个包含 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) . 数组中只有一个重复的数字,但它可能不止重复出现一次.

287. 寻找重复数

题目描述 给定一个包含 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) . 数组中只有一个重复的数字,但它可能不止重复出现一次. 题意 审题可以发现两个关键点: num

LeetCode 287. Find the Duplicate Number (python 判断环,时间复杂度O(n))

LeetCode 287. Find the Duplicate Number 暴力解法 时间 O(nlog(n)),空间O(n),按题目中Note"只用O(1)的空间",照理是过不了的,但是可能判题并没有卡空间复杂度,所以也能AC. class Solution: # 基本思路为,将第一次出现的数字 def findDuplicate(self, nums: List[int]) -> int: s = set() for i in nums: a = i in s if a

[LeetCode] 287. Find the Duplicate Number 寻找重复数

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. Example 1: Input: [1,3,4,2,2

leetcode 287 Find the Duplicate Number寻找重复数

这道题用STL容器就很好写了,可以用set也可以用map, 用unordered_map的C++代码如下: 1 class Solution { 2 public: 3 int findDuplicate(vector<int>& nums) { 4 unordered_map<int, int> m; 5 int res; 6 for(int i=0;i<nums.size();i++){ 7 if(m.count(nums[i])){ 8 res=nums[i];

[LeetCode] Find the Duplicate Number 寻找重复数

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

287 Find the Duplicate Number 寻找重复数

一个长度为 n + 1 的整形数组,其中的数字都在 1 到 n 之间,包括 1 和 n ,可知至少有一个重复的数字存在.假设只有一个数字重复,找出这个重复的数字.注意:    不能更改数组内容(假设数组是只读的).    只能使用恒定的额外空间,即要求空间复杂度是 O(1) .    时间复杂度小于 O(n2)    数组中只有一个数字重复,但它可能不止一次重复出现.详见:https://leetcode.com/problems/find-the-duplicate-number/descri

LeetCode: 153. 寻找旋转排序数组中的最小值(二分查找)

假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] ). 请找出其中最小的元素. 你可以假设数组中不存在重复元素. 示例 1: 输入: [3,4,5,1,2]输出: 1示例 2: 输入: [4,5,6,7,0,1,2]输出: 0 public int findMin(int[] nums) { int lo = 0, hi = nums.length - 1; while (lo < hi) { int

41. First Missing Positive【leetcode】寻找第一个丢失的整数,java,算法

41. First Missing Positive Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. 题目:寻找第一个丢失的整数意思为[1,2,4,5,