-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathRandomizedSet.cs
80 lines (69 loc) · 2.32 KB
/
RandomizedSet.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* ==============================================================================
* 功能描述:IDG
* 创 建 者:gz
* 创建日期:2017/5/10 12:41:14
* ==============================================================================*/
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Threading;
namespace IDGSln
{
/// <summary>
/// IDG
/// </summary>
public class RandomizedSet
{
//insert remove getRandom
private Dictionary<int, int> _dict; //key: value, Value: index
private List<int> _valList; //element of value list, for it can get the O(1) visit
public RandomizedSet()
{
_dict = new Dictionary<int, int>();
_valList = new List<int>();
}
public bool Insert(int val)
{
if (!_dict.ContainsKey(val))
{
_dict.Add(val, _valList.Count);
_valList.Add(val);
return true;
}
return false;
}
public bool Remove(int val)
{
if (!_dict.ContainsKey(val)) return false;
int removeIndex = _dict[val]; //_dict.Values: element index collection
swapToLast(removeIndex);
_valList.Remove(val);
_dict.Remove(val);
return true;
}
//i-待移除元素的索引
//将待移除元素移到_valList的末尾,修改原来末尾元素在字典中的键值
private void swapToLast(int i)
{
int lastIndex = _valList.Count - 1;
if (i > lastIndex) return;
int lastVal = _valList[lastIndex];
int tmpVal = _valList[i];
_valList[i] = lastVal; // the key is to prove " i < lastIndex" !!!
_valList[lastIndex] = tmpVal;
//this is important, to update the last index value in dictionary
_dict[lastVal] = i;
}
public int GetRandom()
{
long tick = DateTime.Now.Ticks;
Random ran = new Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
int randnum = ran.Next(_valList.Count);
if (_valList.Count == 0) return -1;
Thread.Sleep(20);
return _valList[randnum];
}
}
}