数组中重复的数字

来源:互联网 发布:郭天祥单片机教程 编辑:程序博客网 时间:2024/05/29 11:40

在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是重复的数字2或者3。

解题思路:

首先说明几个注意事项:

1、在C++中map 是可以添加元素的,在添加元素的时候,采用的是最下方的代码中的方法时,可以添加重复的元素。但是,采用insert方法插入时,C++的解释为:

pair<iterator,bool> insert ( const value_type& x );           iterator insert ( iterator position, const value_type& x );template <class InputIterator>               void insert ( InputIterator first, InputIterator last );
Themap container is extended by inserting asingle new element (if parameterx is used) or a sequence of elements (if input iterators are used).

Becausemap containers do not allow for duplicate key values, the insertion operation checks for each element inserted whether another element exists already in the container with the same key value, if so, the element is not inserted and its mapped value is not changed in any way.
解释以下,大体说的意思就是,map容器不允许采用insert的方式插入重复元素,所以insert函数具有返回值,来判定原容器中是否存在此元素,若存在,则不插入,并有一个返回值。

举例:

// map::insert#include <iostream>#include <map>using namespace std;int main (){  map<char,int> mymap;  map<char,int>::iterator it;  pair<map<char,int>::iterator,bool> ret;  // first insert function version (single parameter):  mymap.insert ( pair<char,int>('a',100) );  mymap.insert ( pair<char,int>('z',200) );  ret=mymap.insert (pair<char,int>('z',500) );   if (ret.second==false)  {    cout << "element 'z' already existed";    cout << " with a value of " << ret.first->second << endl;  }  // second insert function version (with hint position):  it=mymap.begin();  mymap.insert (it, pair<char,int>('b',300));  // max efficiency inserting  mymap.insert (it, pair<char,int>('c',400));  // no max efficiency inserting  // third insert function version (range insertion):  map<char,int> anothermap;  anothermap.insert(mymap.begin(),mymap.find('c'));  // showing contents:  cout << "mymap contains:\n";  for ( it=mymap.begin() ; it != mymap.end(); it++ )    cout << (*it).first << " => " << (*it).second << endl;  cout << "anothermap contains:\n";  for ( it=anothermap.begin() ; it != anothermap.end(); it++ )    cout << (*it).first << " => " << (*it).second << endl;  return 0;} 
输出为:

element 'z' already existed with a value of 200mymap contains:a => 100b => 300c => 400z => 200anothermap contains:a => 100b => 300

2、在遍历map容器时,采用的是iterator的方式。如何获取容器中的元素呢?

下面举例说明:

// map::begin/end#include <iostream>#include <map>using namespace std;int main (){  map<char,int> mymap;  map<char,int>::iterator it;  mymap['b'] = 100;  mymap['a'] = 200;  mymap['c'] = 300;  // show content:  for ( it=mymap.begin() ; it != mymap.end(); it++ )    <span style="color:#ff0000;">cout << (*it).first << " => " << (*it).second << endl;</span>  return 0;} 
输出如下:

a => 200b => 100c => 300
综上,在解决牛客网上,数组中重复元素时,采用的代码如下:

<pre name="code" class="cpp">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 falsebool duplicate(int numbers[], int length, int* duplication) {map<int,int> assistMap ;/************************************************************************//* 遍历原数组,将数组中的元素添加到map的Key中,利用map的value记录Key出现的次数 *//************************************************************************/for (int i = 0 ; i < length ; i++){assistMap[numbers[i]]++;}/************************************************************************//* 迭代器访问 map中的元素,如果map中的value值大于1,证明此Key 重复出现。记录此Key值,并返回*//************************************************************************/map<int,int>::iterator mapIt ;for(mapIt = assistMap.begin();mapIt != assistMap.end();mapIt++){if ((*mapIt).second> 1){duplication[0] =(*mapIt).first ;return true; }}return false ;}};
                                             
0 0