how to do a dictionary reverse lookup

前端 未结 5 1528
悲哀的现实
悲哀的现实 2021-02-05 04:23

I have a dictionary of type and for a particular case, I need to do a reverse lookup. So for instance suppose I have this entry <\"S

5条回答
  •  被撕碎了的回忆
    2021-02-05 05:15

    1) Keys are unique, values are not. For a given value you have a set of keys.

    2) Lookup by key is O(log n). Iterating with foreach or LINQ is O(n).

    So,
    Option A: Iterate with LINQ, spend O(n) per request, no additional memory.
    Option B: Maintain Dictionary>, spend O(log n) per request, use O(n) additional memory. (There are two suboptions: build this dictionary before a series of look-ups; maintain it all the time)

提交回复
热议问题