【发布时间】:2020-10-11 11:51:20
【问题描述】:
假设我有以下代码片段:
for(int i=0; i < n.length(); i++) {
int aux = n[i];
if(map.containsKey(aux)) {
map.put(aux, map.get(aux)+1);
} else {
map.put(aux, 1);
}
}
我的地图是 HashMap。 我知道 for 将是 O(n),然后 map 操作有 O(1),但是我在那里有三个 map 操作(containsKey、put 和 get)所以是 O(3n) 还是 O(n)?为什么?
【问题讨论】:
标签: java algorithm hashmap time-complexity complexity-theory