Skip to content

Commit 1a06c0e

Browse files
authored
feat: add rust solution to lc problem: No.0470 (doocs#1354)
* feat: add rust solution to lc problem: No.0470 * fix: update
1 parent a06b375 commit 1a06c0e

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

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

+23
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,29 @@ function rand10(): number {
176176
}
177177
```
178178

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+
179202
### **...**
180203

181204
```

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

+23
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,29 @@ function rand10(): number {
139139
}
140140
```
141141

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+
142165
### **...**
143166

144167
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}

0 commit comments

Comments
 (0)