{"id":2131,"date":"2018-03-16T03:36:19","date_gmt":"2018-03-16T10:36:19","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2131"},"modified":"2018-03-16T03:36:32","modified_gmt":"2018-03-16T10:36:32","slug":"leetcode-384-shuffle-an-array","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-384-shuffle-an-array\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 384. Shuffle an Array"},"content":{"rendered":"<p>Shuffle a set of numbers without duplicates.<\/p>\n<p><b>Example:<\/b><\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Init an array with set 1, 2, and 3.\r\nint[] nums = {1,2,3};\r\nSolution solution = new Solution(nums);\r\n\r\n\/\/ Shuffle the array [1,2,3] and return its result. Any permutation of [1,2,3] must equally likely to be returned.\r\nsolution.shuffle();\r\n\r\n\/\/ Resets the array back to its original configuration [1,2,3].\r\nsolution.reset();\r\n\r\n\/\/ Returns the random shuffling of array [1,2,3].\r\nsolution.shuffle();<\/pre>\n<p>C++<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 249 ms\r\nclass Solution {\r\npublic:\r\n  Solution(vector&lt;int&gt; nums) {\r\n    nums_ = std::move(nums);\r\n  }\r\n\r\n  \/** Resets the array to its original configuration and return it. *\/\r\n  vector&lt;int&gt; reset() {\r\n    return nums_;\r\n  }\r\n\r\n  \/** Returns a random shuffling of the array. *\/\r\n  vector&lt;int&gt; shuffle() {\r\n    vector&lt;int&gt; output(nums_);\r\n    const int n = nums_.size();\r\n    for (int i = 0; i &lt; n - 1; ++i) {      \r\n      int j = rand() % (n - i) + i;\r\n      std::swap(output[i], output[j]);\r\n    }\r\n    return output;\r\n  }\r\nprivate:\r\n  vector&lt;int&gt; nums_;\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Shuffle a set of numbers without duplicates. Example: \/\/ Init an array with set 1, 2, and 3. int[] nums = {1,2,3}; Solution solution =&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[177,121,255,256],"class_list":["post-2131","post","type-post","status-publish","format-standard","hentry","category-array","tag-medium","tag-permutation","tag-randomization","tag-shuffle","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2131","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=2131"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2131\/revisions"}],"predecessor-version":[{"id":2133,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2131\/revisions\/2133"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}