本文共 4188 字,大约阅读时间需要 13 分钟。
c语言++数组名【数字】
Problem statement: Write a C++ program to print all the non-repeated numbers in an array in minimum time complexity.
问题陈述:编写一个C ++程序, 以最小的时间复杂度将所有未重复的数字打印在数组中 。
Input Example:
输入示例:
Array length: 10 Array input: 2 5 3 2 4 5 3 6 7 3 Output: Non-repeated numbers are: 7, 6, 4
Solution
解
Data structures used:
使用的数据结构:
Unordered_map
Key in the map is array value
映射中的键是数组值
Value of key is frequency
关键值是频率
Algorithm:
算法:
Declare a map hash to store array elements as keys and to associate their frequencies with them.
声明地图哈希,以将数组元素存储为键并将其频率与它们关联。
Unordered_maphash;
For each array element
对于每个数组元素
Insert it as key & increase frequencies. (0 ->1)
将其作为键插入并增加频率。 (0-> 1)
For same key it will only increase frequencies.
对于相同的键,只会增加频率。
For i=0: n-1 hash[array [i]]++;End For
Now to print the non-repeated character we need to print the keys (array elements) having value (frequency) exactly 1. (Non-repeating)
现在要打印非重复字符,我们需要打印值(频率)正好为1的键(数组元素)。(非重复)
Set an iterator to
将迭代器设置为
hash.begin().
hash.begin() 。
iterator->first is the key (array element) & iterator->second is the value( frequency of corresponding array value)
iterator-> first是键(数组元素)& iterator-> second是值(对应数组值的频率)
IF Iterator->second > 1 Print iterator->first (the array element)END IF
Time complexity: O(n)
时间复杂度:O(n)
Explanation with example:
举例说明:
For this array: 2 5 3 2 4 5 3 6 7 3
对于此阵列: 2 5 3 2 4 5 3 6 7 3
The code:
代码:
for(int i=0;i
Actually does the following
实际上是以下
At i=0 array[i]=2 Insert 2 & increase frequency Hash: Key(element) Value(frequency) 2 1 At i=1 array[i]=5 Insert 5 & increase frequency Hash: Key(element) Value(frequency) 2 1 5 1 At i=2 array[i]=3 Insert 3 & increase frequency Hash: Key(element) Value(frequency) 2 1 5 1 3 1 At i=3 array[i]=2 Insert 2 increase frequency '2' is already there, thus frequency increase. Hash: Key(element) Value(frequency) 2 2 5 1 3 1 At i=4 array[i]=4 Insert 4 &increase frequency Hash: Key(element) Value(frequency) 2 2 5 1 3 1 4 1 At i=5 array[i]=5 '5' is already there, thus frequency increase. Hash: Key(element) Value(frequency) 2 2 5 2 3 1 4 1 At i=6 array[i]=3 '3' is already there, thus frequency increase. Hash: Key(element) Value(frequency) 2 2 5 2 3 2 4 1 At i=7 array[i]=6 Insert 6, increase frequency. Hash: Key(element) Value(frequency) 2 2 5 2 3 2 4 1 6 1 At i=8 array[i]=7 Insert 7, increase frequency. Hash: Key(element) Value(frequency) 2 2 5 2 3 2 4 1 6 1 7 1 At i=9 array[i]=3 '3' is already there, thus frequency increase. Hash: Key(element) Value(frequency) 2 2 5 2 3 3 4 1 6 1 7 1
Thus, Elements with frequency 1 are: 7, 6, 4
因此,频率为1的元素为:7、6、4
#includeusing namespace std;void findNonRepeat(int* a, int n){ //Declare the map unordered_map hash; for(int i=0;i first == key(element value) //iterator->second == value(frequency) for(auto it=hash.begin();it!=hash.end();it++) if(it->second==1)//frequency==1 means non-repeating element printf("%d ",it->first); printf("\n"); }int main(){ int n; cout<<"enter array length\n"; cin>>n; int* a=(int*)(malloc(sizeof(int)*n)); cout<<"input array elements...\n"; for(int i=0;i
Output
输出量
enter array length10input array elements...2 5 3 2 4 5 3 6 7 3the nonrepeating numbers are: 7 6 4
翻译自:
c语言++数组名【数字】
转载地址:http://hwxzd.baihongyu.com/