File tree Expand file tree Collapse file tree 3 files changed +56
-1
lines changed
Expand file tree Collapse file tree 3 files changed +56
-1
lines changed Original file line number Diff line number Diff line change 130130448. 找到所有数组中消失的数字
131131461. 汉明距离
132132463. 岛屿的周长
133+ 470. 用 Rand7() 实现 Rand10()
133134474. 一和零
134135491. 递增子序列
135136494. 目标和
Original file line number Diff line number Diff line change 209209
210210十、数学
21121169. x 的平方根(二分查找)
212-
212+ 470. 用 Rand7() 实现 Rand10()(拒绝采样)
213213
214214十一、图
215215207. 课程表(拓扑排序)
Original file line number Diff line number Diff line change 1+ // 470. 用 Rand7() 实现 Rand10()
2+
3+
4+ /**
5+ * The rand7() API is already defined in the parent class SolBase.
6+ * public int rand7();
7+ * @return a random integer in the range 1 to 7
8+ */
9+
10+
11+ /*
12+ 拒绝采样:
13+ (rand7()-1)*7 等概率产生 0 7 14 21 28 35 42
14+ rand7()-1 等概率产生 0 1 2 3 4 5 6。这里减1目的是缩小产生数字范围,减少拒绝采样量,增大命中率
15+ 两者相加则等概率产生 0-42,在大的等概率空间中取小范围的数,拒绝采样40-42,剩下 0-39 数量上刚好为 10 的倍数,除以10取余加1就可以得到随机数[1,10]
16+ 0-9、10-19、20-29、30-39
17+ */
18+ class Solution extends SolBase {
19+ public int rand10 () {
20+ while (true ) {
21+ int num = (rand7 () - 1 ) * 7 + rand7 () - 1 ;
22+ if (num < 40 ) {
23+ return num % 10 + 1 ;
24+ }
25+ }
26+ }
27+ }
28+
29+
30+ /*
31+ 拒绝采样:
32+ 1、去掉最后的1-9,剩下的数都是1-10等概率产生
33+ 2、return公式理解:错误公式 ((a - 1) * 7 + b) % 10,假如刚好是2行3列的10,则会返回0,但最终要求返回[1,10],所以要先b-1,最后再+1
34+
35+ b 1 2 3 4 5 6 7
36+ a
37+ 1 1 2 3 4 5 6 7
38+ 2 8 9 10 1 2 3 4
39+ 3 5 6 7 8 9 10 1
40+ 4 2 3 4 5 6 7 8
41+ 5 9 10 1 2 3 4 5
42+ 6 6 7 8 9 10 1 2
43+ 7 3 4 5 6 7 8 9
44+ */
45+ class Solution extends SolBase {
46+ public int rand10 () {
47+ while (true ) {
48+ int a = rand7 (), b = rand7 ();
49+ if ((a == 6 && b < 6 ) || a < 6 ) {
50+ return ((a - 1 ) * 7 + b - 1 ) % 10 + 1 ;
51+ }
52+ }
53+ }
54+ }
You can’t perform that action at this time.
0 commit comments