您的位置:首页 > 其它

数组中重复的数字

2016-04-27 21:33 393 查看
class Solution {
public:
// Parameters:
//        numbers:     an array of integers
//        length:      the length of array numbers
//        duplication: (Output) the duplicated number in the array number
// Return value:       true if the input is valid, and there are some duplications in the array number
//                     otherwise false
bool duplicate(int numbers[], int length, int* duplication) {
bool res=false;
if(length==0) return res;
vector<int> num(length,0);
for(int i=0;i<length;i++)
{
if(numbers[i]>=0&&numbers[i]<=length-1)
{
num[numbers[i]]++;
if(num[numbers[i]]>1)
{
*duplication=numbers[i];
res=true;
}
}
else return false;
}
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: