forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
41 lines (33 loc) · 872 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/// Source : https://leetcode.com/problems/implement-rand10-using-rand7/description/
/// Author : liuyubobobo
/// Time : 2018-07-16
#include <iostream>
#include <cassert>
using namespace std;
// The rand7() API is already defined for you.
// int rand7();
// @return a random integer in the range 1 to 7
int rand7(){
return rand() % 7 + 1;
}
/// Rejection Sampling
/// The accurate expectation for calling number of rand7() can be seen here:
/// https://leetcode.com/problems/implement-rand10-using-rand7/solution/
///
/// Time Complexity: O(1)
/// Space Complexity: O(1)
class Solution {
public:
int rand10() {
do{
int randNum = 7 * (rand7() - 1) + rand7() - 1;
if(randNum <= 39)
return randNum % 10 + 1;
}while(true);
assert(false);
return -1;
}
};
int main() {
return 0;
}