problem0432

package
v0.0.0-...-a601ee6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 25, 2021 License: MIT Imports: 1 Imported by: 0

README

432. All O`one Data Structure

题目

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.

解题思路

见程序注释 100

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AllOne

type AllOne struct {
	// contains filtered or unexported fields
}

AllOne 是解题所需的结构

func Constructor

func Constructor() AllOne

Constructor initialize your data structure here.

func (*AllOne) Dec

func (a *AllOne) Dec(key string)

Dec decrements an existing key by 1. If Key's value is 1, remove it from the data structure.

func (*AllOne) GetMaxKey

func (a *AllOne) GetMaxKey() string

GetMaxKey returns one of the keys with maximal value.

func (*AllOne) GetMinKey

func (a *AllOne) GetMinKey() string

GetMinKey returns one of the keys with Minimal value.

func (*AllOne) Inc

func (a *AllOne) Inc(key string)

Inc inserts a new key <Key> with value 1. Or increments an existing key by 1.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL