File tree 2 files changed +29
-0
lines changed
2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change 376
376
466|[ Count The Repetitions] ( ./0466-count-the-repetitions.js ) |Hard|
377
377
467|[ Unique Substrings in Wraparound String] ( ./0467-unique-substrings-in-wraparound-string.js ) |Medium|
378
378
468|[ Validate IP Address] ( ./0468-validate-ip-address.js ) |Medium|
379
+ 470|[ Implement Rand10() Using Rand7()] ( ./0470-implement-rand10-using-rand7.js ) |Medium|
379
380
472|[ Concatenated Words] ( ./0472-concatenated-words.js ) |Hard|
380
381
476|[ Number Complement] ( ./0476-number-complement.js ) |Easy|
381
382
482|[ License Key Formatting] ( ./0482-license-key-formatting.js ) |Easy|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments