🔴力扣原题:
🟠题目简述:
给定一个包含 n + 1 个整数的数组 nums ,其数字都在 [1, n] 范围内(包括 1 和 n),可知至少存在一个重复的整数。
假设 nums 只有 一个重复的整数 ,返回 这个重复的数 。
你设计的解决方案必须 不修改 数组 nums 且只用常量级 O(1) 的额外空间。
🟡解题思路:
map
来计数;- over;
🟢C++代码:
class Solution {
public:
int findDuplicate(vector<int>& nums) {
map<int, int> map;
for(auto num : nums)
{
++map[num];
}
for(auto it = map.begin(); it != map.end();it++)
{
//cout << it->first << it->second << endl;
if(it->second >1)
{
return it->first;
}
}
return 0;
}
};