Skip to content

Commit ccba755

Browse files
committedMar 1, 2025
Add solution #470
1 parent fde7591 commit ccba755

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@
376376
466|[Count The Repetitions](./0466-count-the-repetitions.js)|Hard|
377377
467|[Unique Substrings in Wraparound String](./0467-unique-substrings-in-wraparound-string.js)|Medium|
378378
468|[Validate IP Address](./0468-validate-ip-address.js)|Medium|
379+
470|[Implement Rand10() Using Rand7()](./0470-implement-rand10-using-rand7.js)|Medium|
379380
472|[Concatenated Words](./0472-concatenated-words.js)|Hard|
380381
476|[Number Complement](./0476-number-complement.js)|Easy|
381382
482|[License Key Formatting](./0482-license-key-formatting.js)|Easy|
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 470. Implement Rand10() Using Rand7()
3+
* https://leetcode.com/problems/implement-rand10-using-rand7/
4+
* Difficulty: Medium
5+
*
6+
* Given the API rand7() that generates a uniform random integer in the range [1, 7],
7+
* write a function rand10() that generates a uniform random integer in the range [1, 10].
8+
* You can only call the API rand7(), and you shouldn't call any other API. Please do not
9+
* use a language's built-in random API.
10+
*
11+
* Each test case will have one internal argument n, the number of times that your implemented
12+
* function rand10() will be called while testing. Note that this is not an argument passed to
13+
* rand10().
14+
*/
15+
16+
/**
17+
* The rand7() API is already defined for you.
18+
* var rand7 = function() {}
19+
* @return {number} a random integer in the range 1 to 7
20+
*/
21+
var rand10 = function() {
22+
while (true) {
23+
const n = (rand7() - 1) * 7 + rand7();
24+
if (n <= 40) {
25+
return (n % 10) + 1;
26+
}
27+
}
28+
};

0 commit comments

Comments
 (0)
Please sign in to comment.