Skip to content

Commit a06b375

Browse files
authored
feat: add solutions to lc problem: No.0470 (doocs#1353)
No.0470.Implement Rand10() Using Rand7()
1 parent 2e5e66c commit a06b375

File tree

7 files changed

+266
-2
lines changed

7 files changed

+266
-2
lines changed

solution/0400-0499/0470.Implement Rand10() Using Rand7()/README.md

+100-1
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,121 @@
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62+
**方法一:拒绝采样**
63+
64+
我们可以使用拒绝采样的方法实现等概率生成任意区间的随机数。拒绝采样的思路是如果生成的随机数落在我们希望的区间内,那么就返回该随机数,否则会不断生成直到生成一个落在区间内的随机数为止。
65+
66+
对于本题,我们可以通过调用 $rand7()$ 两次来实现生成 $[1,10]$ 以内的随机数,具体如下:
67+
68+
我们生成一个大于等于 $1$ 且小于等于 $40$ 的整数 $x$,其中等概率生成的方式为 $x = (rand7() - 1) \times 7 + rand7()$,然后,我们返回 $x \bmod 10 + 1$ 即可。
69+
70+
期望时间复杂度为 $O(1)$,但是最坏情况下会达到无穷大的时间复杂度。空间复杂度为 $O(1)$。
71+
6272
<!-- tabs:start -->
6373

6474
### **Python3**
6575

6676
<!-- 这里可写当前语言的特殊实现逻辑 -->
6777

6878
```python
69-
79+
# The rand7() API is already defined for you.
80+
# def rand7():
81+
# @return a random integer in the range 1 to 7
82+
83+
84+
class Solution:
85+
def rand10(self):
86+
"""
87+
:rtype: int
88+
"""
89+
while 1:
90+
i = rand7() - 1
91+
j = rand7()
92+
x = i * 7 + j
93+
if x <= 40:
94+
return x % 10 + 1
7095
```
7196

7297
### **Java**
7398

7499
<!-- 这里可写当前语言的特殊实现逻辑 -->
75100

76101
```java
102+
/**
103+
* The rand7() API is already defined in the parent class SolBase.
104+
* public int rand7();
105+
* @return a random integer in the range 1 to 7
106+
*/
107+
class Solution extends SolBase {
108+
public int rand10() {
109+
while (true) {
110+
int i = rand7() - 1;
111+
int j = rand7();
112+
int x = i * 7 + j;
113+
if (x <= 40) {
114+
return x % 10 + 1;
115+
}
116+
}
117+
}
118+
}
119+
```
120+
121+
### **C++**
122+
123+
```cpp
124+
// The rand7() API is already defined for you.
125+
// int rand7();
126+
// @return a random integer in the range 1 to 7
127+
128+
class Solution {
129+
public:
130+
int rand10() {
131+
while (1) {
132+
int i = rand7() - 1;
133+
int j = rand7();
134+
int x = i * 7 + j;
135+
if (x <= 40) {
136+
return x % 10 + 1;
137+
}
138+
}
139+
}
140+
};
141+
```
142+
143+
### **Go**
144+
145+
```go
146+
func rand10() int {
147+
for {
148+
i := rand7() - 1
149+
j := rand7()
150+
x := i*7 + j
151+
if x <= 40 {
152+
return x%10 + 1
153+
}
154+
}
155+
}
156+
```
77157

158+
### **TypeScript**
159+
160+
```ts
161+
/**
162+
* The rand7() API is already defined for you.
163+
* function rand7(): number {}
164+
* @return a random integer in the range 1 to 7
165+
*/
166+
167+
function rand10(): number {
168+
while (true) {
169+
const i = rand7() - 1;
170+
const j = rand7();
171+
const x = i * 7 + j;
172+
if (x <= 40) {
173+
return (x % 10) + 1;
174+
}
175+
}
176+
}
78177
```
79178

80179
### **...**

solution/0400-0499/0470.Implement Rand10() Using Rand7()/README_EN.md

+90-1
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,102 @@
4141
### **Python3**
4242

4343
```python
44-
44+
# The rand7() API is already defined for you.
45+
# def rand7():
46+
# @return a random integer in the range 1 to 7
47+
48+
49+
class Solution:
50+
def rand10(self):
51+
"""
52+
:rtype: int
53+
"""
54+
while 1:
55+
i = rand7() - 1
56+
j = rand7()
57+
x = i * 7 + j
58+
if x <= 40:
59+
return x % 10 + 1
4560
```
4661

4762
### **Java**
4863

4964
```java
65+
/**
66+
* The rand7() API is already defined in the parent class SolBase.
67+
* public int rand7();
68+
* @return a random integer in the range 1 to 7
69+
*/
70+
class Solution extends SolBase {
71+
public int rand10() {
72+
while (true) {
73+
int i = rand7() - 1;
74+
int j = rand7();
75+
int x = i * 7 + j;
76+
if (x <= 40) {
77+
return x % 10 + 1;
78+
}
79+
}
80+
}
81+
}
82+
```
83+
84+
### **C++**
85+
86+
```cpp
87+
// The rand7() API is already defined for you.
88+
// int rand7();
89+
// @return a random integer in the range 1 to 7
90+
91+
class Solution {
92+
public:
93+
int rand10() {
94+
while (1) {
95+
int i = rand7() - 1;
96+
int j = rand7();
97+
int x = i * 7 + j;
98+
if (x <= 40) {
99+
return x % 10 + 1;
100+
}
101+
}
102+
}
103+
};
104+
```
105+
106+
### **Go**
107+
108+
```go
109+
func rand10() int {
110+
for {
111+
i := rand7() - 1
112+
j := rand7()
113+
x := i*7 + j
114+
if x <= 40 {
115+
return x%10 + 1
116+
}
117+
}
118+
}
119+
```
50120

121+
### **TypeScript**
122+
123+
```ts
124+
/**
125+
* The rand7() API is already defined for you.
126+
* function rand7(): number {}
127+
* @return a random integer in the range 1 to 7
128+
*/
129+
130+
function rand10(): number {
131+
while (true) {
132+
const i = rand7() - 1;
133+
const j = rand7();
134+
const x = i * 7 + j;
135+
if (x <= 40) {
136+
return (x % 10) + 1;
137+
}
138+
}
139+
}
51140
```
52141

53142
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// The rand7() API is already defined for you.
2+
// int rand7();
3+
// @return a random integer in the range 1 to 7
4+
5+
class Solution {
6+
public:
7+
int rand10() {
8+
while (1) {
9+
int i = rand7() - 1;
10+
int j = rand7();
11+
int x = i * 7 + j;
12+
if (x <= 40) {
13+
return x % 10 + 1;
14+
}
15+
}
16+
}
17+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
func rand10() int {
2+
for {
3+
i := rand7() - 1
4+
j := rand7()
5+
x := i*7 + j
6+
if x <= 40 {
7+
return x%10 + 1
8+
}
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* The rand7() API is already defined in the parent class SolBase.
3+
* public int rand7();
4+
* @return a random integer in the range 1 to 7
5+
*/
6+
class Solution extends SolBase {
7+
public int rand10() {
8+
while (true) {
9+
int i = rand7() - 1;
10+
int j = rand7();
11+
int x = i * 7 + j;
12+
if (x <= 40) {
13+
return x % 10 + 1;
14+
}
15+
}
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# The rand7() API is already defined for you.
2+
# def rand7():
3+
# @return a random integer in the range 1 to 7
4+
5+
6+
class Solution:
7+
def rand10(self):
8+
"""
9+
:rtype: int
10+
"""
11+
while 1:
12+
i = rand7() - 1
13+
j = rand7()
14+
x = i * 7 + j
15+
if x <= 40:
16+
return x % 10 + 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* The rand7() API is already defined for you.
3+
* function rand7(): number {}
4+
* @return a random integer in the range 1 to 7
5+
*/
6+
7+
function rand10(): number {
8+
while (true) {
9+
const i = rand7() - 1;
10+
const j = rand7();
11+
const x = i * 7 + j;
12+
if (x <= 40) {
13+
return (x % 10) + 1;
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)