{"id":3199,"date":"2018-07-16T09:08:23","date_gmt":"2018-07-16T16:08:23","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3199"},"modified":"2018-07-16T23:37:02","modified_gmt":"2018-07-17T06:37:02","slug":"leetcode-872-implement-rand10-using-rand7","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/desgin\/leetcode-872-implement-rand10-using-rand7\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 872. Implement Rand10() Using Rand7()"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given a function\u00a0<code>rand7<\/code>\u00a0which generates a uniform random integer in the range 1 to 7, write a function\u00a0<code>rand10<\/code>\u00a0which generates a uniform random integer in the range 1 to 10.<\/p>\n<p>Do NOT use system&#8217;s\u00a0<code>Math.random()<\/code>.<\/p>\n<div>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-1-1\">1<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-1\">[7]<\/span>\r\n<\/pre>\n<div>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-2-1\">2<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-2\">[8,4]<\/span>\r\n<\/pre>\n<div>\n<p><strong>Example 3:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong><span id=\"example-input-3-1\">3<\/span>\r\n<strong>Output: <\/strong><span id=\"example-output-3\">[8,1,10]<\/span>\r\n<\/pre>\n<p><strong>Note:<\/strong><\/p>\n<ol>\n<li><code>rand7<\/code>\u00a0is predefined.<\/li>\n<li>Each testcase has one argument:\u00a0<code>n<\/code>, the number of times that\u00a0<code>rand10<\/code>\u00a0is called.<\/li>\n<\/ol>\n<h1><strong>Solution: Math<\/strong><\/h1>\n<p>Time complexity: O(49\/40) = O(1)<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 112 ms\r\nclass Solution {\r\npublic:\r\n  int rand10() {\r\n    int target = 40;\r\n    while (target &gt;= 40)\r\n      target = 7 * (rand7() - 1) + (rand7() - 1);\r\n    return target % 10 + 1;\r\n  }\r\n};<\/pre>\n<p>Time complexity: O(7\/6 + 7 \/ 5) = O(1)<\/p>\n<pre class=\"lang:default decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 112 ms\r\nclass Solution {\r\npublic:\r\n  int rand10() {\r\n    int i = INT_MAX;\r\n    int j = INT_MAX;\r\n    while (i &gt; 6) i = rand7(); \/\/ i = [1, 2, 3, 4, 5, 6]\r\n    while (j &gt; 5) j = rand7(); \/\/ j = [1, 2, 3, 4, 5]\r\n    return j + 5 * (i &amp; 1);\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a function\u00a0rand7\u00a0which generates a uniform random integer in the range 1 to 7, write a function\u00a0rand10\u00a0which generates a uniform random integer in the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[340],"tags":[330,31,177,91],"class_list":["post-3199","post","type-post","status-publish","format-standard","hentry","category-desgin","tag-desgin","tag-math","tag-medium","tag-random","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3199","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3199"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3199\/revisions"}],"predecessor-version":[{"id":3203,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3199\/revisions\/3203"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3199"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3199"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3199"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}