File tree 3 files changed +64
-0
lines changed
solution/0400-0499/0470.Implement Rand10() Using Rand7()
3 files changed +64
-0
lines changed Original file line number Diff line number Diff line change @@ -176,6 +176,29 @@ function rand10(): number {
176
176
}
177
177
```
178
178
179
+ ### ** Rust**
180
+
181
+ ``` rust
182
+ /**
183
+ * The rand7() API is already defined for you.
184
+ * @return a random integer in the range 1 to 7
185
+ * fn rand7() -> i32;
186
+ */
187
+
188
+ impl Solution {
189
+ pub fn rand10 () -> i32 {
190
+ loop {
191
+ let i = rand7 () - 1 ;
192
+ let j = rand7 ();
193
+ let x = i * 7 + j ;
194
+ if x <= 40 {
195
+ return (x % 10 ) + 1 ;
196
+ }
197
+ }
198
+ }
199
+ }
200
+ ```
201
+
179
202
### ** ...**
180
203
181
204
```
Original file line number Diff line number Diff line change @@ -139,6 +139,29 @@ function rand10(): number {
139
139
}
140
140
```
141
141
142
+ ### ** Rust**
143
+
144
+ ``` rust
145
+ /**
146
+ * The rand7() API is already defined for you.
147
+ * @return a random integer in the range 1 to 7
148
+ * fn rand7() -> i32;
149
+ */
150
+
151
+ impl Solution {
152
+ pub fn rand10 () -> i32 {
153
+ loop {
154
+ let i = rand7 () - 1 ;
155
+ let j = rand7 ();
156
+ let x = i * 7 + j ;
157
+ if x <= 40 {
158
+ return (x % 10 ) + 1 ;
159
+ }
160
+ }
161
+ }
162
+ }
163
+ ```
164
+
142
165
### ** ...**
143
166
144
167
```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * The rand7() API is already defined for you.
3
+ * @return a random integer in the range 1 to 7
4
+ * fn rand7() -> i32;
5
+ */
6
+
7
+ impl Solution {
8
+ pub fn rand10 ( ) -> i32 {
9
+ loop {
10
+ let i = rand7 ( ) - 1 ;
11
+ let j = rand7 ( ) ;
12
+ let x = i * 7 + j;
13
+ if x <= 40 {
14
+ return ( x % 10 ) + 1 ;
15
+ }
16
+ }
17
+ }
18
+ }
You can’t perform that action at this time.
0 commit comments