数组中重复的数字
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 | /* 在一个长度位n的数组里面所有的数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字重复了,也不知道 每个数字重复了几次。找到数组中任意一个重复的数字。例如,如果 输入长度为7的数组{2,3,1,0,2,5,3},那么对应的重复数组就是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; } /* 解法2:不用另外再申请空间,就能完成的解法。如果第i个位置存放i, 那么因为数组中有重复的数字,那么必然有的位置存在多个数字。 从头到尾依次扫描这个数组中的每一个数字,当扫描到下标为i的数字m时,比较这个数字m 是不是等于i。如果是,扫描下一个数字,如果不是,拿它与第m位置的数字进行比较,如果重复 找到结果,如果不重复,交换位置。接下来再重复这个比较、交换的过程,直到发现一个重复的数字。 */ #include<iostream> using namespace std; bool duplicate( int numbers[], int length, int * duplicate) { if (numbers == nullptr || length == 0) { return 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]]) { *duplicate = numbers[i]; return true ; } int tmp = numbers[i]; numbers[i] = numbers[tmp]; numbers[tmp] = tmp; } } return false ; } int main() { int nums[] = { 2,3,1,0,2,5,3 }; //要查询的数组 int result = -1; duplicate(nums, sizeof (nums) / sizeof (nums[0]), &result); cout << "duplicate = " << result << endl; return 0; } |
posted on 2021-10-17 23:02 xcxfury001 阅读(36) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用