1. 程式人生 > >[leetcode]432. All O`one Data Structure全O(1)數據結構

[leetcode]432. All O`one Data Structure全O(1)數據結構

lex new ati 數據 support move operation func hal

Implement a data structure supporting the following operations:

  1. Inc(Key) - Inserts a new key with value 1. Or increments an existing key by 1. Key is guaranteed to be a non-empty string.
  2. Dec(Key) - If Key‘s value is 1, remove it from the data structure. Otherwise decrements an existing key by 1. If the key does not exist, this function does nothing. Key is guaranteed to be a non-empty string.
  3. GetMaxKey() - Returns one of the keys with maximal value. If no element exists, return an empty string "".
  4. GetMinKey() - Returns one of the keys with minimal value. If no element exists, return an empty string "".

Challenge: Perform all these in O(1) time complexity.

題意:

設計一個類似hash map的計數器,但要提供最大值對應鍵值、最小值對應鍵值的功能。

Solution1:

code

[leetcode]432. All O`one Data Structure全O(1)數據結構