美文网首页
offer03数组中重复的数字

offer03数组中重复的数字

作者: D_w | 来源:发表于2022-03-17 18:04 被阅读0次

在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次。请找出数组中任意一个重复的数字。

示例 1:

输入:
[2, 3, 1, 0, 2, 5, 3]
输出:2 或 3

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解法:
一,利用字典,时间复杂度O(N),空间复杂度O(N)
1.初始化: 新建 HashSet ,记为 dicdic ;
2.遍历数组 nums 中的每个数字 numnum :
当 num 在 dicdic 中,说明重复,直接返回 num ;
将 num 添加至 dic 中;
3.返回 -1 。本题中一定有重复数字,因此这里返回多少都可以。
python

class Solution:
    def findRepeatNumber(self, nums: List[int]) -> int:
        dic = {}
        for i in nums:
            if i in dic:
                return i
            else:
                dic[i] = 1
        return -1

java

public class Offer03 {
    public int findRepeatNumber(int[] nums) {
        Set<Integer>dic = new HashSet<>();
        for (int num:nums){
            if (dic.contains(num)){
                return num;
            }
            dic.add(num);
        }
        return -1;
    }
}

解法二:原地交换 时间复杂度 O(N),空间复杂度O(1)
遍历数组 numsnums ,设索引初始值为 i = 0i=0 :

若 nums[i] = inums[i]=i : 说明此数字已在对应索引位置,无需交换,因此跳过;
若 nums[nums[i]] = nums[i]nums[nums[i]]=nums[i] : 代表索引 nums[i]nums[i] 处和索引 ii 处的元素值都为 nums[i]nums[i] ,即找到一组重复值,返回此值 nums[i]nums[i] ;
否则: 交换索引为 ii 和 nums[i]nums[i] 的元素值,将此数字交换至对应索引位置。
若遍历完毕尚未返回,则返回 -1−1 。
python

class Solution:
    def findRepeatNumber(self, nums) -> int:
        n = len(nums)
        for i in range(n):
            while i != nums[i]:
                if nums[i] == nums[nums[i]]:
                    return nums[i]
                temp = nums[i]
                nums[i], nums[temp] = nums[temp], nums[i]

java

class Solution {
    public int findRepeatNumber(int[] nums) {
        int i = 0;
        while(i < nums.length) {
            if(nums[i] == i) {
                i++;
                continue;
            }
            if(nums[nums[i]] == nums[i]) return nums[i];
            int tmp = nums[i];
            nums[i] = nums[tmp];
            nums[tmp] = tmp;
        }
        return -1;
    }
}

相关文章

  • offer03数组中重复的数字

    在一个长度为 n 的数组 nums 里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复的,但不知道有几个...

  • 剑指offer题集

    [3] 数组中重复的数字 题目一:找出数组中重复的数字 Description 在一个长度为n的数组里的所有数字都...

  • LeetCode 每日一题 [38] 数组中重复的数字

    LeetCode 数组中重复的数字 [简单] 找出数组中重复的数字。在一个长度为 n 的数组 nums 里的所有数...

  • 剑指offer4J【C2 P3】找出数组中重复数字

    题目 找出数组中重复的数字数组中数字都在0~n之间,其中有些数字是重复的,但不知道谁重复,可能有1到多个重复的数字...

  • 面试题03. 数组中重复的数字

    数组中重复的数字 题目描述 找出数组中重复的数字。 在一个长度为 n 的数组 nums 里的所有数字都在 0~n-...

  • 数组中重复的数字

    题目一:找出数组中重复的数字 在一个长度为 n 的数组里的所有数字都在 0~n-1 的范围内。数组中某些数字是重复...

  • 剑指offer学习笔记:8.1 数组

    面试题51:数组中重复的数字在一个长度为n的数组中,所有数字都在0到n-1的范围内。数组中的某些数字是重复的,但是...

  • 数组中重复的数字

    题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重...

  • 数组中重复的数字

    数组中重复的数字 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个...

  • 数组中重复的数字

    题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重...

网友评论

      本文标题:offer03数组中重复的数字

      本文链接:https://www.haomeiwen.com/subject/ehavdrtx.html