leetcode 384. Shuffle an Array
384. Shuffle an Array
c++ random函数:https://www.jb51.net/article/124108.htm
rand()不需要参数,它会返回一个从0到最大随机数的任意整数,最大随机数的大小通常是固定的一个大整数。 这样,如果你要产生0~10的10个整数,可以表达为:
int N = rand() % 11;
这样,N的值就是一个0~10的随机数,如果要产生1~10,则是这样:
总结来说,可以表示为:
a + rand() % n
其中的a是起始值,n是整数的范围。
https://www.cnblogs.com/grandyang/p/5783392.html
Knuth shuffle算法:
https://yjk94.wordpress.com/2017/03/17/%E6%B4%97%E7%89%8C%E7%9A%84%E6%AD%A3%E7%A1%AE%E5%A7%BF%E5%8A%BF-knuth-shuffle%E7%AE%97%E6%B3%95/
不能直接随机取数字,只能随机取之前的数字才能保证等概率。
注意:取余i+1才能取到为i的余数。遍历到i的时候,必须有交换i自己这一项,所以必须是i+1。
https://leetcode.com/problems/shuffle-an-array/discuss/85965/C++-Knuth-Shuffle-(Fisher-Yates-Shuffle)-Implementation-(352-ms)
class Solution {
public:
Solution(vector<int> nums){
for(auto num : nums)
v.push_back(num);
}
/** Resets the array to its original configuration and return it. */
vector<int> reset() {
return v;
}
/** Returns a random shuffling of the array. */
vector<int> shuffle() {
vector<int> res = v;
for (int i = ; i < res.size(); ++i) {
int t = rand() % (i + );
swap(res[i], res[t]);
}
return res;
}
private:
vector<int> v;
};
leetcode 384. Shuffle an Array的更多相关文章
- [LeetCode] 384. Shuffle an Array 数组洗牌
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- Java [Leetcode 384]Shuffle an Array
题目描述: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...
- 【LeetCode】384. Shuffle an Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数 Fisher–Yates 洗牌 水塘抽样 日 ...
- 384. Shuffle an Array
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- 384. Shuffle an Array数组洗牌
[抄题]: Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. i ...
- LC 384. Shuffle an Array
Shuffle a set of numbers without duplicates. Example: // Init an array with set 1, 2, and 3. int[] n ...
- 384 Shuffle an Array 打乱数组
打乱一个没有重复元素的数组.示例:// 以数字集合 1, 2 和 3 初始化数组.int[] nums = {1,2,3};Solution solution = new Solution(nums) ...
- 384. Shuffle an Array(java,数组全排列,然后随机取)
题目: Shuffle a set of numbers without duplicates. 分析: 对一组不包含重复元素的数组进行随机重排,reset方法返回最原始的数组,shuffle方法随机 ...
- leetcode mock Shuffle an Array
1. shuffle算法: http://www.cnblogs.com/huaping-audio/archive/2008/09/09/1287985.html 注意:我们一般用的是第二种swap ...
随机推荐
- SecurityProtocolType 枚举
地址:https://docs.microsoft.com/zh-cn/dotnet/api/system.net.securityprotocoltype?redirectedfrom=MSDN&a ...
- Windows连接Linux服务器远程开发解决方案
解决方案 vscode+Linux服务器 解决连接问题 vscode商店下载remote-ssh工具,然后进行配置. 这个网上依旧有很多详细的教程了,这里就不过多赘述. 配置免密登录 这一部分是我要重 ...
- Linux关闭防火墙、设置端口
关闭防火墙 1)重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off 验证防火墙是否关闭:chkconfig --list |grep ...
- 人脸识别调用返回http
https://ai.baidu.com/docs#/Face-Detect-V3/top
- LG5487 【模板】线性递推+BM算法
[模板]线性递推+BM算法 给出一个数列 \(P\) 从 \(0\) 开始的前 \(n\) 项,求序列 \(P\) 在\(\bmod~998244353\) 下的最短线性递推式,并在 \(\bmod~ ...
- *.net框架 - IEnumerable类 & IQueryable类
什么使用IQueryable<T> 查询返回类型为什么用IQueryable<T>,而不用 IEnumerable<T>类型? IQueryable接口实现IEnu ...
- NOIP2013积木大赛 [贪心]
大意 自己查去... 说明 这道题正解是贪心,但标程里是有这样一句话的:把序列分成(a1,..ai)(ai+1,...aj)......(ak,...an)多个非递减序列.然后所有段中最大值的和减去除 ...
- Golang 内存管理
- Java【基础学习】之暴力求素数【用数组返回】
Java[基础学习]之暴力求素数[用数组返回] */ import java.util.*; public class Main{ public static void main(String[] a ...
- CentOS6.5 安装ES5.5
一.CURL查看已开启的ES es5.5:elasticsearch-5.5.2.tar.gz下载,百度云地址 https://pan.baidu.com/s/17oFOQlePLtUhhJHxEPR ...