What is the best way to maintain a mapping of each element in the ARRAY to its index? A hash table.
The hash table reduces the look up time to O(1).If a collision occurred, a look up could degenerate to O(n) time.
Realization:
Map<Integer,Integer> map=new HashMap<Integer,Integer>();
插入:
map.put(key,value)
查询key是否存在(返回bool型):
map.containsKey(key)
根据key查找value:
value = map.get(key);