当前位置:网站首页>leetcode-384.打乱数组
leetcode-384.打乱数组
2022-07-22 18:06:00 【KGundam】
数学问题-随机与取样
题目详情
给你一个整数数组 nums ,设计算法来打乱一个没有重复元素的数组。打乱后,数组的所有排列应该是 等可能 的。
实现 Solution class:Solution(int[] nums) 使用整数数组 nums 初始化对象int[] reset() 重设数组到它的初始状态并返回int[] shuffle() 返回数组随机打乱后的结果
示例1:
输入
["Solution", "shuffle", "reset", "shuffle"]
[[[1, 2, 3]], [], [], []]
输出
[null, [3, 1, 2], [1, 2, 3], [1, 3, 2]]
解释
Solution solution = new Solution([1, 2, 3]);
solution.shuffle(); // 打乱数组 [1,2,3] 并返回结果。任何 [1,2,3]的排列返回的概率应该相同。例如,返回 [3, 1, 2]
solution.reset(); // 重设数组到它的初始状态 [1, 2, 3] 。返回 [1, 2, 3]
solution.shuffle(); // 随机返回数组 [1, 2, 3] 打乱后的结果。例如,返回 [1, 3, 2]
思路:
可以利用 Fisher-Yates 洗牌算法,原理是通过随机交换位置来实现随机打乱,有正向
和反向两种写法,且实现非常方便。
洗牌算法的思想是(以反向洗牌为例):每次从未处理的数据中随机取出一个数字,然后把该数字放在数组的尾部,即数组尾部存放的是已经处理过的数字。具体实现如下:
我的代码:
class Solution
{
//vector<int> nums;
vector<int> origin;
public:
Solution(vector<int>& nums): origin(std::move(nums)) {
}
/*这里等价于↓,目的是将nums拷贝至origin Solution(vector<int>& nums) { //这种方法还需要借助一个类内的nums数组,因为传入的nums是引用形式 this->nums = nums; this->origin.resize(nums.size()); copy(nums.begin(), nums.end(), origin.begin()); } */
vector<int> reset()
{
return origin;
}
vector<int> shuffle()
{
if (origin.empty()) return {
};
vector<int> shuffled(origin);
int n = origin.size();
//反向洗牌
for (int i = n - 1; i >= 0; --i)
{
//每次交换末尾的数和 0~末尾之间的某个随机数
swap(shuffled[i], shuffled[rand() % (i + 1)]);
}
/*正向洗牌 for (int i = 0; i < n; ++i) { //pos是0 ~ n-i-1的一个随机数 int pos = rand() % (n - i); //每次交换首部的数和 i~n-1之间的某个随机数 swap(shuffled[i], shuffled[i+pos]); } */
return shuffled;
}
};
/** * Your Solution object will be instantiated and called as such: * Solution* obj = new Solution(nums); * vector<int> param_1 = obj->reset(); * vector<int> param_2 = obj->shuffle(); */
边栏推荐
- GIC Introduction (II) - use of gic400
- 用 sed 去除文件中的 ASCII 控制字符乱码
- 51nod 1677 treecnt (tree DP, inverse element, contribution)
- 什么是DOM
- anaconda关闭不了进行强制关闭的方法
- Using tensorflow to preprocess the input image to get the input layer of neural network
- Druid源码阅读7-DruidDataSource的recycle过程
- linux centos7环境下修改oracle19c监听IP并重启
- Druid source code read the status in 9-druiddatasource and druidconnection
- 事件委托&&表单验证&&正则表达式
猜你喜欢
随机推荐
VSCode 环境配置管理
BOM&DOM
Introduction to zo1x (functional safety verification)
Druid source code read the getconnection process of 4-druiddatasource
Druid源码阅读3-DruidDataSource连接池的基本原理
Druid source code read 6-preparedstatementpool source code and usage scenario analysis
Espressif 8266 AT+MQTT连接AWS IoT
DOM - node operation
uniapp项目打包为桌面应用的方法步骤
WPS mathtype安装 错误53
CreateProcess 输出重定向
verilog 避坑指南(持续更新)
Redis script scan
File preview (access local resources through URL)
A new look at linear classifiers from a geometric perspective
JS - - date Object & Ternary expression
Emotional tendency calculation based on Baidu AI Cloud platform
51nod 1685 k-th interval 2 (tree array, reverse order pair)
架构训练营模块一作业
EOJ monthly 2021.2 sponsored by tusimple a









