Push, pop, top, and retrieving the bottom element in constant time.
push (x) - Push element x onto stack.
pop () - Removes the element.
top () - Get the top element.
getMin () - Retrieve the minimum element in the stack.
MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); --> Returns -3. minStack.pop(); minStack.top(); --> Returns 0. minStack.getMin(); --> Returns -2.
O (1) time.
insert (val): inserts an item val to the set if not already present.
remove (val): remove the item from the set if present.
getRandom: Returns a random element from the current set of elements. Each element must have been returned.
// Init an empty set. RandomizedSet randomSet = new RandomizedSet(); // Inserts 1 to the set. Returns true as 1 was inserted successfully. randomSet.insert(1); // Returns false as 2 does not exist in the set. randomSet.remove(2); // Inserts 2 to the set, returns true. Set now contains [1,2]. randomSet.insert(2); // getRandom should return either 1 or 2 randomly. randomSet.getRandom(); // Removes 1 from the set, returns true. Set now contains [2]. randomSet.remove(1); // 2 was already in the set, so return false. randomSet.insert(2); // Since 2 is the only number in the set, getRandom always return 2. randomSet.getRandom();
It is a process that can be carried out at the same time or the computer environment.
Design a binary tree. There is no restriction on how your serialization / deserialization algorithm should work. This can be deserialized to the original tree structure.
For example, you may serialize the following tree
one
/ \
2 3
/ \
4 5
as "[1,2,3, null, null, 4,5]", serializes a binary tree. You must be creative and come up with different approaches.
Note: Do not use a class member to store states. Your serialize and deserialize algorithms should not be stateless.
Source: https://habr.com/ru/post/329208/
All Articles