Problem statement:
Design a data structure that supports all following operations in average O(1) time.
insert(val)
: Inserts an item val to the set if not already present.remove(val)
: Removes an item val from the set if present.getRandom
: Returns a random element from current set of elements. Each element must have the same probability of being returned.
Go over the website Java code, write a C# version:
http://www.programcreek.com/2014/08/leetcode-insert-delete-getrandom-o1-java/
Java code from the above webpage:
https://gist.github.com/jianminchen/b6ab09a8a047db42da4ad663036d621a
C# code written by Julia
https://gist.github.com/jianminchen/9ebfbe7462c17689344578be31c804c0
Highlights of practice:
1. Spent 20+ minutes to write the function remove(int val)
2. Put test cases into main function, test the code to understand the problem.
3. Did not understand the design, why two hashmaps are used.
4. Concern about function remove - line 133 - 165
The function is not readable - one function does so many things - break Single Responsibility Principle.
Java code from the above webpage:
https://gist.github.com/jianminchen/b6ab09a8a047db42da4ad663036d621a
C# code written by Julia
https://gist.github.com/jianminchen/9ebfbe7462c17689344578be31c804c0
Highlights of practice:
1. Spent 20+ minutes to write the function remove(int val)
2. Put test cases into main function, test the code to understand the problem.
3. Did not understand the design, why two hashmaps are used.
4. Concern about function remove - line 133 - 165
The function is not readable - one function does so many things - break Single Responsibility Principle.
No comments:
Post a Comment