From fa60bf8348b583069da194c5eef81a1e65db9f3e Mon Sep 17 00:00:00 2001 From: ShusenTang Date: Thu, 19 Dec 2019 17:34:31 +0800 Subject: [PATCH] Create 384. Shuffle an Array.md --- solutions/384. Shuffle an Array.md | 57 ++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 solutions/384. Shuffle an Array.md diff --git a/solutions/384. Shuffle an Array.md b/solutions/384. Shuffle an Array.md new file mode 100644 index 0000000..d5ae5d9 --- /dev/null +++ b/solutions/384. Shuffle an Array.md @@ -0,0 +1,57 @@ +# [384. Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/) + +# 思路 +给定一个没有重复元素数组,将其随机打乱,即洗牌。 + +## 思路一、暴力 +先看最暴力的解法,想象一下我们先将所有牌放到一个黑箱里面,然后不断从中不放回地随机抽取一张,这样最后得到的就是洗好的牌。 + +实现上我们可以用一个循环,第一循环时从整个数组中随机选一个元素然后将这个元素从数组中删掉;第二次循环再从中选出一个元素.....。 + +因为一共进行了n次循环,每次将数组中的元素删除是线性复杂度的,所以时间复杂度O(n^2)。 + +## 思路二、Fisher-Yates 洗牌算法 + +思路一每次循环的删除操作是线性复杂度的,可以优化至常数复杂度:我们每次将要删除的元素和最末尾的元素交换,然后删除末尾元素(其实不用删除,记录一下就是)。 + +优化后的暴力法就很类似Fisher-Yates洗牌算法了:在每次迭代中,生成一个范围在当前下标到数组末尾元素下标之间的随机下标。 +接下来,将当前元素和随机选出的下标所指的元素互相交换(这一步模拟了每次从黑箱里面摸一张牌的过程),这样每次被随机选中的元素都不会再被选中(相当于已经删除了)。 + +> 注意:当前元素是可以和它本身互相交换的。 + +时间复杂度O(n) + +# C++ +## 思路二 +``` C++ +class Solution { +private: + vectornums_origin; + vectornums_shuffle; + int size; +public: + Solution(vector& nums) { + nums_origin = nums; + nums_shuffle = nums; + size = nums.size(); + } + + /** Resets the array to its original configuration and return it. */ + vector reset() { + return nums_origin; + } + + /** Fisher-Yates 洗牌算法 */ + vector shuffle() { + // 反向迭代 + for(int i = size - 1; i >= 0; i--) + swap(nums_shuffle[i], nums_shuffle[rand() % (i + 1)]); + // 正向迭代 + // for(int i = 0; i < size; i++){ + // int rand_idx = rand() % (size - i) + i; + // swap(nums_shuffle[i], nums_shuffle[rand_idx]); + // } + return nums_shuffle; + } +}; +```