不修改数组找出重复的数字

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* 在一个长度为n+1的数组里的所有的数字都在1~n的范围内,所以数组中
* 至少有一个数字是重复的。请找出数组中任意一个重复的数字,但不能修改
* 输入的数组。例如,如果输入长度为8的数组{2,3,5,4,3,2,6,7},那么对应的
* 输出是重复的数字2或者3.
*/
 
/*
解法1:利用一个哈希表,从头到尾扫描数组中的每一个数字,得出结果。
时间复杂度为O(n),但是最坏情况下要创建一个n-1个大小的哈希表为代价。
*/
 
#include<iostream>
#include<unordered_map>
 
using namespace std;
 
unordered_map<int, int> g_hash_map;
 
//判断在哈希表中是否有重复的元素
inline bool is_duplicate(const int& num)
{
    if (g_hash_map.find(num) == g_hash_map.end())
    {
        g_hash_map[num] = 1;
        return false;
    }
 
    return true;
}
 
int main()
{
    int nums[] = { 2,3,1,0,2,5,3 };   //要查询的数组
    //遍历数组
    for (auto ite = begin(nums); ite != end(nums); ite++)
    {
        if (is_duplicate(*ite))
        {
            cout << "duplicate num = " << *ite << endl;
            break;
        }
    }
 
    return 0;
}
 
/*
* 解法二:不需要O(n)的辅助空间,也不需要修改数组。按照二分查找的思想
* 但是时间复杂度为O(nlogn)。
* 我们把1~n的数字从中间的数字m分为两部分,前面一半为1~m,后面为m+1~n。
* 遍历整个数组,统计1~m中元素的数量,如果大于m个,则这个范围内必定有重复的数字。
* 如果没有,则统计数组m+1~n中元素的数量。这样把包含重复数字的区间一分为二,直到找到一个
* 重复的数字。
*/
 
#include<iostream>
using namespace std;
 
//统计数组中一定范围内的数字数量
int countRange(const int* numbers, int length, int start, int end)
{
    if (numbers == nullptr)
        return 0;
 
    int count = 0;
    for (int i = 0; i < length; ++i)
    {
        if (numbers[i] >= start && numbers[i] <= end)
        {
            ++count;
        }
    }
 
    return count;
}
 
 
int getDuplication(const int* numbers, int length)
{
    if (numbers == nullptr || length <= 0)
        return -1;
 
    int start = 1;
    int end = length - 1;
    while (start <= end)
    {
        int middle = start + ((end - start) >> 1);
        int count = countRange(numbers, length, start, middle);
        if ((end == start) && (count > 1))
        {
            return start;
        }
 
        if (count > (middle - start + 1))
        {
            end = middle;
        }
        else
        {
            start = middle+1;
        }
    }
    return -1;
}
 
int main()
{
    int nums[] = { 2,3,5,4,3,2,6,7 };   //要查询的数组
 
    int result = -1;
 
    result = getDuplication(nums, sizeof(nums) / sizeof(nums[0]));
 
    if (result != -1)
    {
        cout << "duplicate number = " << result << endl;
    }
    else
    {
        cout << "Do not have duplicate number." << endl;
    }
 
    return 0;
}

  

posted on   xcxfury001  阅读(36)  评论(0编辑  收藏  举报

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示