?LeetCode刷題實戰(zhàn)470:用 Rand7() 實現(xiàn) Rand10()
Given the API rand7() that generates a uniform random integer in the range [1, 7], write a function rand10() that generates a uniform random integer in the range [1, 10]. You can only call the API rand7(), and you shouldn't call any other API. Please do not use a language's built-in random API.
Each test case will have one internal argument n, the number of times that your implemented function rand10() will be called while testing. Note that this is not an argument passed to rand10().
示例? ? ? ? ? ? ? ? ? ? ? ? ?
示例 1:
輸入: 1
輸出: [7]
示例 2:
輸入: 2
輸出: [8,4]
示例 3:
輸入: 3
輸出: [8,1,10]
解題
class?Solution?{
public:
????int?rand10()?{
????????int?ans = 0;
????????do{
????????????ans = (rand7() - 1) * 7?+ rand7();
????????}while(ans > 40);
????????return?ans % 10?+ 1;
????}
};
LeetCode刷題實戰(zhàn)462:最少移動次數(shù)使數(shù)組元素相等 II
LeetCode刷題實戰(zhàn)465:最優(yōu)賬單平衡
LeetCode刷題實戰(zhàn)466:統(tǒng)計重復(fù)個數(shù)
LeetCode刷題實戰(zhàn)467:環(huán)繞字符串中唯一的子字符串
