Skip to content

Commit ab65727

Browse files
committed
https://leetcode.com/problems/insert-delete-getrandom-o1/
1 parent b123934 commit ab65727

File tree

1 file changed

+2
-62
lines changed

1 file changed

+2
-62
lines changed

MediumProblems/Insert Delete GetRandom.cs

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
/// <summary>
44
/// https://leetcode.com/problems/insert-delete-getrandom-o1/
55
/// </summary>
6-
public class RandomizedSet2
6+
public class RandomizedSet
77
{
88
private HashSet<int> _set;
99
private List<int> _nums;
1010
private Random _random;
1111
/** Initialize your data structure here. */
12-
public RandomizedSet2()
12+
public RandomizedSet()
1313
{
1414
_set = new HashSet<int>();
1515
_nums = new List<int>();
@@ -50,64 +50,4 @@ public int GetRandom()
5050
return _nums[_random.Next(_nums.Count)];
5151
}
5252
}
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-
}
11353
}

0 commit comments

Comments
 (0)