51 数组中重复的数字

哈希

时间On 空间On

bool duplicate(int numbers[], int length, int* duplication) {
    unordered_map<int, int> count;
    for (int i = 0; i < length; i++)
        count[numbers[i]]++;
    unordered_map<int, int>::iterator ite = count.begin();
    while (ite != count.end())
    {
        if (ite->second != 1)
        {
            *duplication = ite->first;
            return true;
        }
        ite++;
    }
    return false;
}

方法二

时间On空间O1

bool duplicate(int numbers[], int length, int* duplication)
{
    if (numbers == NULL || length <= 0)
        retrun false;
    for (int i = 0; i < length; i++)    //根据题意检查数字合法性
    {
        if (numbers[i] < 0 || numbers[i] > length - 1)
            return false;
    }
    for (int i = 0; i < length; i++)
    {
        while (numbers[i] != i)
        {
            if (numbers[i] == numbers[numbers[i]])    //找到重复数字
            {
                *duplication = numbers[i];
                return true;
            }
            swap(numbers[i], numbers[numbers[i]]); //放到该放的位置
        }
    }
    return false;
}
时间: 2024-08-07 20:57:46

51 数组中重复的数字的相关文章

面试题51 数组中重复的数字

题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3. 1 class Solution { 2 public: 3 // Parameters: 4 // numbers: an array of integers 5 // length: the length of arr

剑指Offer面试题51(Java版):数组中重复的数字

题目:在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复的次数.请找出数组中任意一个重复的数字. 例如如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3. 解决这个问题的一个简单的方法是先把输入的数组排序.从排序的数组中找出重复的数字是件容易的事情,只需要从头到尾扫描排序后的数组就可以了.排序一个长度为n的数组需要时间为O(nlogn)时间. 还可以利用哈希表来解决这个问题,从头到尾

查找数组中重复的数字

题目来源于<剑指Offer>中的面试题3:找出数组中重复的数字. // 题目:在一个长度为n的数组里的所有数字都在0到n-1的范围内.数组中某些数字是重复的,但不知道有几个数字重复了, // 也不知道每个数字重复了几次.请找出数组中任意一个重复的数字.例如,如果输入长度为7的数组{2, 3, 1, 0, 2, 5, 3}, // 那么对应的输出是重复的数字2或者3. 解决方法有多种,包括数组排序,哈希表法,以及作者推荐的重排数组法.此处介绍自己的一个做法,以空间换时间,通过新建数组来实现快速查

去掉数组中重复的数字

冒泡排序语法: for (int i = 0; i < 数组长度 - 1; i++) { for (int j = 0; j < 数组长度 - i - 1; j++) { if (数组名[j] < 数组名[j + 1]) { int empty = 数组名[j]; 数组名[j] = 数组名[j + 1]; 数组名[j + 1] = empty; } } } 上面这个语法是降序排序,如果想升序的话就把if(数组名[j]<数组名[j=1])里面的小于号“<”改成大于号“>”

剑指offer之【数组中重复的数字】

题目: 数组中重复的数字 链接: https://www.nowcoder.com/practice/623a5ac0ea5b4e5f95552655361ae0a8?tpId=13&tqId=11203&rp=3&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking 题目描述: 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不

(笔试题)把一个整数数组中重复的数字去掉

题目: 把一个整数数组中重复的数字去掉,并输出剩下的不重复的元素.(要求不能开辟新空间) 思路: 先排序,然后遍历数组比较,详见代码 代码: #include <iostream> #include <algorithm> using namespace std; int cmp(const void* a,const void* b){ return (*(int*)a-*(int*)b); } int unique(int* a,int n){ int k=0; for(int

【剑指offer】3、数组中重复的数字

题目一:找出数组中重复的数字. ......S2 ..... 1 class Solution { 2 public: 3 // Parameters: 4 // numbers: an array of integers 5 // length: the length of array numbers 6 // duplication: (Output) the duplicated number in the array number 7 // Return value: true if t

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

[题目]找出数组中重复的数字: 在一个长度为 n 的数组 nums 里的所有数字**都在 0-n-1 的范围内**.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次.请找出数组中任意一个重复的数字. 示例 1: 输入:[2, 3, 1, 0, 2, 5, 3] 输出:2 或 3 限制:2 <= n <= 100000 解法一(官方): 思路: 由于只需要返回任意一个重复数字即可,则数据结构可采用集合:(集合不允许有重复数据) (1)foreach遍历数组,尝试将其中

【剑指offer】数组中重复的数字

题目链接:数组中重复的数字 题意:在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2. 题解:丢进set,统计,查找,输出.STL大法好! 代码: 1 class Solution { 2 public: 3 // Parameters: 4 // numbers: an array