博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c语言++数组名【数字】_C ++程序在数组中打印所有非重复数字
阅读量:2534 次
发布时间:2019-05-11

本文共 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:

算法:

  1. Declare a map hash to store array elements as keys and to associate their frequencies with them.

    声明地图哈希,以将数组元素存储为键并将其频率与它们关联。

  2. Unordered_map 
    hash;
  3. For each array element

    对于每个数组元素

    Insert it as key & increase frequencies. (0 ->1)

    将其作为键插入并增加频率。 (0-> 1)

    For same key it will only increase frequencies.

    对于相同的键,只会增加频率。

  4. For i=0: n-1	hash[array [i]]++;End For
  5. 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是值(对应数组值的频率)

  6. 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

C ++实现可在数组中按频率打印所有非重复数字 (C++ implementation to print all the Non-Repeated Numbers with Frequency in an Array)

#include 
using 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/

你可能感兴趣的文章
学习使用Django一 安装虚拟环境
查看>>
Hibernate视频学习笔记(8)Lazy策略
查看>>
CSS3 结构性伪类选择器(1)
查看>>
IOS 杂笔-14(被人遗忘的owner)
查看>>
自动测试用工具
查看>>
前端基础之BOM和DOM
查看>>
[T-ARA/筷子兄弟][Little Apple]
查看>>
编译Libgdiplus遇到的问题
查看>>
【NOIP 模拟赛】Evensgn 剪树枝 树形dp
查看>>
java学习笔记④MySql数据库--01/02 database table 数据的增删改
查看>>
两台电脑如何实现共享文件
查看>>
组合模式Composite
查看>>
程序员最想得到的十大证件,你最想得到哪个?
查看>>
我的第一篇CBBLOGS博客
查看>>
【MyBean调试笔记】接口的使用和清理
查看>>
07 js自定义函数
查看>>
jQueru中数据交换格式XML和JSON对比
查看>>
form表单序列化后的数据转json对象
查看>>
[PYTHON]一个简单的单元測试框架
查看>>
iOS开发网络篇—XML数据的解析
查看>>