|
3 | 3 | /// <summary>
|
4 | 4 | /// https://leetcode.com/problems/insert-delete-getrandom-o1/
|
5 | 5 | /// </summary>
|
6 |
| - public class RandomizedSet2 |
| 6 | + public class RandomizedSet |
7 | 7 | {
|
8 | 8 | private HashSet<int> _set;
|
9 | 9 | private List<int> _nums;
|
10 | 10 | private Random _random;
|
11 | 11 | /** Initialize your data structure here. */
|
12 |
| - public RandomizedSet2() |
| 12 | + public RandomizedSet() |
13 | 13 | {
|
14 | 14 | _set = new HashSet<int>();
|
15 | 15 | _nums = new List<int>();
|
@@ -50,64 +50,4 @@ public int GetRandom()
|
50 | 50 | return _nums[_random.Next(_nums.Count)];
|
51 | 51 | }
|
52 | 52 | }
|
53 |
| - |
54 |
| - public class RandomizedSet |
55 |
| - { |
56 |
| - private IDictionary<int, int> _set; |
57 |
| - private List<int> _nums; |
58 |
| - private Random _random; |
59 |
| - |
60 |
| - /** Initialize your data structure here. */ |
61 |
| - public RandomizedSet() |
62 |
| - { |
63 |
| - _set = new Dictionary<int, int>(); |
64 |
| - _nums = new List<int>(); |
65 |
| - _random = new Random(); |
66 |
| - } |
67 |
| - |
68 |
| - /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */ |
69 |
| - public bool Insert(int val) |
70 |
| - { |
71 |
| - if (_set.ContainsKey(val)) |
72 |
| - { |
73 |
| - return false; |
74 |
| - } |
75 |
| - else |
76 |
| - { |
77 |
| - _set.Add(val, _nums.Count); |
78 |
| - _nums.Add(val); |
79 |
| - |
80 |
| - return true; |
81 |
| - } |
82 |
| - } |
83 |
| - |
84 |
| - /** Removes a value from the set. Returns true if the set contained the specified element. */ |
85 |
| - public bool Remove(int val) |
86 |
| - { |
87 |
| - if (_set.ContainsKey(val)) |
88 |
| - { |
89 |
| - // Get the index of the val from Dictionary |
90 |
| - // Replace the val with the last number in the list |
91 |
| - // Update the Dictionary with the new index value for the number being switched |
92 |
| - // Remove the number at the last position in the list |
93 |
| - // Remove the val from dictionary |
94 |
| - var index = _set[val]; |
95 |
| - _nums[index] = _nums[_nums.Count - 1]; |
96 |
| - _set[_nums[_nums.Count - 1]] = index; |
97 |
| - _nums.RemoveAt(_nums.Count -1); |
98 |
| - _set.Remove(val); |
99 |
| - return true; |
100 |
| - } |
101 |
| - else |
102 |
| - { |
103 |
| - return false; |
104 |
| - } |
105 |
| - } |
106 |
| - |
107 |
| - /** Get a random element from the set. */ |
108 |
| - public int GetRandom() |
109 |
| - { |
110 |
| - return _nums[_random.Next(_nums.Count)]; |
111 |
| - } |
112 |
| - } |
113 | 53 | }
|
0 commit comments