C++ hashmap

将数组的 下标、值 作为 值-键 对放入map ,用find函数查找,时间复杂度为o(n)降为o(1)。

拿空间换时间,能达到去重的目的。

当然存放还是需要o(n)的时间复杂度,可用于o(n^2)降为o(n)。

map<int,int> m;

m[arr[index]] = index;

if(m.find(target) != m.end) //找到数组中的目标值,m[target]返回原数组的下标

posted @ 2020-03-29 14:37  比特的脉搏  阅读(1132)  评论(1)    收藏  举报